]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
3ec1a456704d503d732a46b962157eedc8924728
[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   canShare,
32   futureDaysToUnixTime,
33   hostname,
34   isImage,
35   isVideo,
36   mdNoImages,
37   mdToHtml,
38   mdToHtmlInline,
39   myAuthRequired,
40   newVote,
41   numToSI,
42   relTags,
43   setupTippy,
44   share,
45   showScores,
46 } from "../../utils";
47 import { amAdmin } from "../../utils/roles/am-admin";
48 import { amCommunityCreator } from "../../utils/roles/am-community-creator";
49 import { amMod } from "../../utils/roles/am-mod";
50 import { canAdmin } from "../../utils/roles/can-admin";
51 import { canMod } from "../../utils/roles/can-mod";
52 import { isAdmin } from "../../utils/roles/is-admin";
53 import { isBanned } from "../../utils/roles/is-banned";
54 import { isMod } from "../../utils/roles/is-mod";
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       <div 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       </div>
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`}
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-block ${
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         <div
509           className="d-inline-block"
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>
523           {url ? (
524             this.props.showBody ? (
525               <a
526                 className={`d-inline-block ${
527                   !post.featured_community && !post.featured_local
528                     ? "text-body"
529                     : "text-primary"
530                 }`}
531                 href={url}
532                 title={url}
533                 rel={relTags}
534               >
535                 <div
536                   className="d-inline-block"
537                   dangerouslySetInnerHTML={mdToHtmlInline(post.name)}
538                 />
539               </a>
540             ) : (
541               this.postLink
542             )
543           ) : (
544             this.postLink
545           )}
546           {(url && isImage(url)) ||
547             (post.thumbnail_url && (
548               <button
549                 className="btn btn-link text-monospace text-muted small d-inline-block"
550                 data-tippy-content={i18n.t("expand_here")}
551                 onClick={linkEvent(this, this.handleImageExpandClick)}
552               >
553                 <Icon
554                   icon={
555                     !this.state.imageExpanded ? "plus-square" : "minus-square"
556                   }
557                   classes="icon-inline"
558                 />
559               </button>
560             ))}
561           {post.removed && (
562             <small className="ml-2 text-muted font-italic">
563               {i18n.t("removed")}
564             </small>
565           )}
566           {post.deleted && (
567             <small
568               className="unselectable pointer ml-2 text-muted font-italic"
569               data-tippy-content={i18n.t("deleted")}
570             >
571               <Icon icon="trash" classes="icon-inline text-danger" />
572             </small>
573           )}
574           {post.locked && (
575             <small
576               className="unselectable pointer ml-2 text-muted font-italic"
577               data-tippy-content={i18n.t("locked")}
578             >
579               <Icon icon="lock" classes="icon-inline text-danger" />
580             </small>
581           )}
582           {post.featured_community && (
583             <small
584               className="unselectable pointer ml-2 text-muted font-italic"
585               data-tippy-content={i18n.t("featured")}
586             >
587               <Icon icon="pin" classes="icon-inline text-primary" />
588             </small>
589           )}
590           {post.featured_local && (
591             <small
592               className="unselectable pointer ml-2 text-muted font-italic"
593               data-tippy-content={i18n.t("featured")}
594             >
595               <Icon icon="pin" classes="icon-inline text-secondary" />
596             </small>
597           )}
598           {post.nsfw && (
599             <small className="ml-2 text-muted font-italic">
600               {i18n.t("nsfw")}
601             </small>
602           )}
603         </h5>
604       </div>
605     );
606   }
607
608   duplicatesLine() {
609     const dupes = this.props.crossPosts;
610     return dupes && dupes.length > 0 ? (
611       <ul className="list-inline mb-1 small text-muted">
612         <>
613           <li className="list-inline-item mr-2">{i18n.t("cross_posted_to")}</li>
614           {dupes.map(pv => (
615             <li key={pv.post.id} className="list-inline-item mr-2">
616               <Link to={`/post/${pv.post.id}`}>
617                 {pv.community.local
618                   ? pv.community.name
619                   : `${pv.community.name}@${hostname(pv.community.actor_id)}`}
620               </Link>
621             </li>
622           ))}
623         </>
624       </ul>
625     ) : (
626       <></>
627     );
628   }
629
630   commentsLine(mobile = false) {
631     const post = this.postView.post;
632
633     return (
634       <div className="d-flex justify-content-start flex-wrap text-muted font-weight-bold mb-1">
635         {this.commentsButton}
636         {canShare() && (
637           <button
638             className="btn btn-link"
639             onClick={linkEvent(this, this.handleShare)}
640             type="button"
641           >
642             <Icon icon="share" inline />
643           </button>
644         )}
645         {!post.local && (
646           <a
647             className="btn btn-link btn-animate text-muted py-0"
648             title={i18n.t("link")}
649             href={post.ap_id}
650           >
651             <Icon icon="fedilink" inline />
652           </a>
653         )}
654         {mobile && !this.props.viewOnly && this.mobileVotes}
655         {UserService.Instance.myUserInfo &&
656           !this.props.viewOnly &&
657           this.postActions(mobile)}
658       </div>
659     );
660   }
661
662   postActions(mobile = false) {
663     // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
664     // Possible enhancement: Make each button a component.
665     const post_view = this.postView;
666     return (
667       <>
668         {this.saveButton}
669         {this.crossPostButton}
670         {mobile && this.showMoreButton}
671         {(!mobile || this.state.showAdvanced) && (
672           <>
673             {!this.myPost && (
674               <>
675                 {this.reportButton}
676                 {this.blockButton}
677               </>
678             )}
679             {this.myPost && (this.showBody || this.state.showAdvanced) && (
680               <>
681                 {this.editButton}
682                 {this.deleteButton}
683               </>
684             )}
685           </>
686         )}
687         {this.state.showAdvanced && (
688           <>
689             {this.showBody && post_view.post.body && this.viewSourceButton}
690             {/* Any mod can do these, not limited to hierarchy*/}
691             {(amMod(this.props.moderators) || amAdmin()) && (
692               <>
693                 {this.lockButton}
694                 {this.featureButton}
695               </>
696             )}
697             {(this.canMod_ || this.canAdmin_) && <>{this.modRemoveButton}</>}
698           </>
699         )}
700         {!mobile && this.showMoreButton}
701       </>
702     );
703   }
704
705   get commentsButton() {
706     const post_view = this.postView;
707     return (
708       <Link
709         className="btn btn-link text-muted py-0 pl-0 text-muted"
710         title={i18n.t("number_of_comments", {
711           count: Number(post_view.counts.comments),
712           formattedCount: Number(post_view.counts.comments),
713         })}
714         to={`/post/${post_view.post.id}?scrollToComments=true`}
715       >
716         <Icon icon="message-square" classes="mr-1" inline />
717         <span className="mr-2">
718           {i18n.t("number_of_comments", {
719             count: Number(post_view.counts.comments),
720             formattedCount: numToSI(post_view.counts.comments),
721           })}
722         </span>
723         {this.unreadCount && (
724           <span className="small text-warning">
725             ({this.unreadCount} {i18n.t("new")})
726           </span>
727         )}
728       </Link>
729     );
730   }
731
732   get unreadCount(): number | undefined {
733     const pv = this.postView;
734     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
735       ? undefined
736       : pv.unread_comments;
737   }
738
739   get mobileVotes() {
740     // TODO: make nicer
741     const tippy = showScores()
742       ? { "data-tippy-content": this.pointsTippy }
743       : {};
744     return (
745       <>
746         <div>
747           <button
748             className={`btn-animate btn py-0 px-1 ${
749               this.postView.my_vote === 1 ? "text-info" : "text-muted"
750             }`}
751             {...tippy}
752             onClick={linkEvent(this, this.handleUpvote)}
753             aria-label={i18n.t("upvote")}
754             aria-pressed={this.postView.my_vote === 1}
755           >
756             {this.state.upvoteLoading ? (
757               <Spinner />
758             ) : (
759               <>
760                 <Icon icon="arrow-up1" classes="icon-inline small" />
761                 {showScores() && (
762                   <span className="ml-2">
763                     {numToSI(this.postView.counts.upvotes)}
764                   </span>
765                 )}
766               </>
767             )}
768           </button>
769           {this.props.enableDownvotes && (
770             <button
771               className={`ml-2 btn-animate btn py-0 px-1 ${
772                 this.postView.my_vote === -1 ? "text-danger" : "text-muted"
773               }`}
774               onClick={linkEvent(this, this.handleDownvote)}
775               {...tippy}
776               aria-label={i18n.t("downvote")}
777               aria-pressed={this.postView.my_vote === -1}
778             >
779               {this.state.downvoteLoading ? (
780                 <Spinner />
781               ) : (
782                 <>
783                   <Icon icon="arrow-down1" classes="icon-inline small" />
784                   {showScores() && (
785                     <span
786                       className={classNames("ml-2", {
787                         invisible: this.postView.counts.downvotes === 0,
788                       })}
789                     >
790                       {numToSI(this.postView.counts.downvotes)}
791                     </span>
792                   )}
793                 </>
794               )}
795             </button>
796           )}
797         </div>
798       </>
799     );
800   }
801
802   get saveButton() {
803     const saved = this.postView.saved;
804     const label = saved ? i18n.t("unsave") : i18n.t("save");
805     return (
806       <button
807         className="btn btn-link btn-animate text-muted py-0"
808         onClick={linkEvent(this, this.handleSavePostClick)}
809         data-tippy-content={label}
810         aria-label={label}
811       >
812         {this.state.saveLoading ? (
813           <Spinner />
814         ) : (
815           <Icon
816             icon="star"
817             classes={classNames({ "text-warning": saved })}
818             inline
819           />
820         )}
821       </button>
822     );
823   }
824
825   get crossPostButton() {
826     return (
827       <Link
828         className="btn btn-link btn-animate text-muted py-0"
829         to={{
830           /* Empty string properties are required to satisfy type*/
831           pathname: "/create_post",
832           state: { ...this.crossPostParams },
833           hash: "",
834           key: "",
835           search: "",
836         }}
837         title={i18n.t("cross_post")}
838         data-tippy-content={i18n.t("cross_post")}
839         aria-label={i18n.t("cross_post")}
840       >
841         <Icon icon="copy" inline />
842       </Link>
843     );
844   }
845
846   get reportButton() {
847     return (
848       <button
849         className="btn btn-link btn-animate text-muted py-0"
850         onClick={linkEvent(this, this.handleShowReportDialog)}
851         data-tippy-content={i18n.t("show_report_dialog")}
852         aria-label={i18n.t("show_report_dialog")}
853       >
854         <Icon icon="flag" inline />
855       </button>
856     );
857   }
858
859   get blockButton() {
860     return (
861       <button
862         className="btn btn-link btn-animate text-muted py-0"
863         onClick={linkEvent(this, this.handleBlockPersonClick)}
864         data-tippy-content={i18n.t("block_user")}
865         aria-label={i18n.t("block_user")}
866       >
867         {this.state.blockLoading ? <Spinner /> : <Icon icon="slash" inline />}
868       </button>
869     );
870   }
871
872   get editButton() {
873     return (
874       <button
875         className="btn btn-link btn-animate text-muted py-0"
876         onClick={linkEvent(this, this.handleEditClick)}
877         data-tippy-content={i18n.t("edit")}
878         aria-label={i18n.t("edit")}
879       >
880         <Icon icon="edit" inline />
881       </button>
882     );
883   }
884
885   get deleteButton() {
886     const deleted = this.postView.post.deleted;
887     const label = !deleted ? i18n.t("delete") : i18n.t("restore");
888     return (
889       <button
890         className="btn btn-link btn-animate text-muted py-0"
891         onClick={linkEvent(this, this.handleDeleteClick)}
892         data-tippy-content={label}
893         aria-label={label}
894       >
895         {this.state.deleteLoading ? (
896           <Spinner />
897         ) : (
898           <Icon
899             icon="trash"
900             classes={classNames({ "text-danger": deleted })}
901             inline
902           />
903         )}
904       </button>
905     );
906   }
907
908   get showMoreButton() {
909     return (
910       <button
911         className="btn btn-link btn-animate text-muted py-0"
912         onClick={linkEvent(this, this.handleShowAdvanced)}
913         data-tippy-content={i18n.t("more")}
914         aria-label={i18n.t("more")}
915       >
916         <Icon icon="more-vertical" inline />
917       </button>
918     );
919   }
920
921   get viewSourceButton() {
922     return (
923       <button
924         className="btn btn-link btn-animate text-muted py-0"
925         onClick={linkEvent(this, this.handleViewSource)}
926         data-tippy-content={i18n.t("view_source")}
927         aria-label={i18n.t("view_source")}
928       >
929         <Icon
930           icon="file-text"
931           classes={classNames({ "text-success": this.state.viewSource })}
932           inline
933         />
934       </button>
935     );
936   }
937
938   get lockButton() {
939     const locked = this.postView.post.locked;
940     const label = locked ? i18n.t("unlock") : i18n.t("lock");
941     return (
942       <button
943         className="btn btn-link btn-animate text-muted py-0"
944         onClick={linkEvent(this, this.handleModLock)}
945         data-tippy-content={label}
946         aria-label={label}
947       >
948         {this.state.lockLoading ? (
949           <Spinner />
950         ) : (
951           <Icon
952             icon="lock"
953             classes={classNames({ "text-danger": locked })}
954             inline
955           />
956         )}
957       </button>
958     );
959   }
960
961   get featureButton() {
962     const featuredCommunity = this.postView.post.featured_community;
963     const labelCommunity = featuredCommunity
964       ? i18n.t("unfeature_from_community")
965       : i18n.t("feature_in_community");
966
967     const featuredLocal = this.postView.post.featured_local;
968     const labelLocal = featuredLocal
969       ? i18n.t("unfeature_from_local")
970       : i18n.t("feature_in_local");
971     return (
972       <span>
973         <button
974           className="btn btn-link btn-animate text-muted py-0 pl-0"
975           onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
976           data-tippy-content={labelCommunity}
977           aria-label={labelCommunity}
978         >
979           {this.state.featureCommunityLoading ? (
980             <Spinner />
981           ) : (
982             <span>
983               <Icon
984                 icon="pin"
985                 classes={classNames({ "text-success": featuredCommunity })}
986                 inline
987               />
988               {i18n.t("community")}
989             </span>
990           )}
991         </button>
992         {amAdmin() && (
993           <button
994             className="btn btn-link btn-animate text-muted py-0"
995             onClick={linkEvent(this, this.handleModFeaturePostLocal)}
996             data-tippy-content={labelLocal}
997             aria-label={labelLocal}
998           >
999             {this.state.featureLocalLoading ? (
1000               <Spinner />
1001             ) : (
1002               <span>
1003                 <Icon
1004                   icon="pin"
1005                   classes={classNames({ "text-success": featuredLocal })}
1006                   inline
1007                 />
1008                 {i18n.t("local")}
1009               </span>
1010             )}
1011           </button>
1012         )}
1013       </span>
1014     );
1015   }
1016
1017   get modRemoveButton() {
1018     const removed = this.postView.post.removed;
1019     return (
1020       <button
1021         className="btn btn-link btn-animate text-muted py-0"
1022         onClick={linkEvent(
1023           this,
1024           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
1025         )}
1026       >
1027         {/* TODO: Find an icon for this. */}
1028         {this.state.removeLoading ? (
1029           <Spinner />
1030         ) : !removed ? (
1031           i18n.t("remove")
1032         ) : (
1033           i18n.t("restore")
1034         )}
1035       </button>
1036     );
1037   }
1038
1039   /**
1040    * Mod/Admin actions to be taken against the author.
1041    */
1042   userActionsLine() {
1043     // TODO: make nicer
1044     const post_view = this.postView;
1045     return (
1046       this.state.showAdvanced && (
1047         <>
1048           {this.canMod_ && (
1049             <>
1050               {!this.creatorIsMod_ &&
1051                 (!post_view.creator_banned_from_community ? (
1052                   <button
1053                     className="btn btn-link btn-animate text-muted py-0"
1054                     onClick={linkEvent(
1055                       this,
1056                       this.handleModBanFromCommunityShow
1057                     )}
1058                     aria-label={i18n.t("ban_from_community")}
1059                   >
1060                     {i18n.t("ban_from_community")}
1061                   </button>
1062                 ) : (
1063                   <button
1064                     className="btn btn-link btn-animate text-muted py-0"
1065                     onClick={linkEvent(
1066                       this,
1067                       this.handleModBanFromCommunitySubmit
1068                     )}
1069                     aria-label={i18n.t("unban")}
1070                   >
1071                     {this.state.banLoading ? <Spinner /> : i18n.t("unban")}
1072                   </button>
1073                 ))}
1074               {!post_view.creator_banned_from_community && (
1075                 <button
1076                   className="btn btn-link btn-animate text-muted py-0"
1077                   onClick={linkEvent(this, this.handleAddModToCommunity)}
1078                   aria-label={
1079                     this.creatorIsMod_
1080                       ? i18n.t("remove_as_mod")
1081                       : i18n.t("appoint_as_mod")
1082                   }
1083                 >
1084                   {this.state.addModLoading ? (
1085                     <Spinner />
1086                   ) : this.creatorIsMod_ ? (
1087                     i18n.t("remove_as_mod")
1088                   ) : (
1089                     i18n.t("appoint_as_mod")
1090                   )}
1091                 </button>
1092               )}
1093             </>
1094           )}
1095           {/* Community creators and admins can transfer community to another mod */}
1096           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1097             this.canAdmin_) &&
1098             this.creatorIsMod_ &&
1099             (!this.state.showConfirmTransferCommunity ? (
1100               <button
1101                 className="btn btn-link btn-animate text-muted py-0"
1102                 onClick={linkEvent(
1103                   this,
1104                   this.handleShowConfirmTransferCommunity
1105                 )}
1106                 aria-label={i18n.t("transfer_community")}
1107               >
1108                 {i18n.t("transfer_community")}
1109               </button>
1110             ) : (
1111               <>
1112                 <button
1113                   className="d-inline-block mr-1 btn btn-link btn-animate text-muted py-0"
1114                   aria-label={i18n.t("are_you_sure")}
1115                 >
1116                   {i18n.t("are_you_sure")}
1117                 </button>
1118                 <button
1119                   className="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
1120                   aria-label={i18n.t("yes")}
1121                   onClick={linkEvent(this, this.handleTransferCommunity)}
1122                 >
1123                   {this.state.transferLoading ? <Spinner /> : i18n.t("yes")}
1124                 </button>
1125                 <button
1126                   className="btn btn-link btn-animate text-muted py-0 d-inline-block"
1127                   onClick={linkEvent(
1128                     this,
1129                     this.handleCancelShowConfirmTransferCommunity
1130                   )}
1131                   aria-label={i18n.t("no")}
1132                 >
1133                   {i18n.t("no")}
1134                 </button>
1135               </>
1136             ))}
1137           {/* Admins can ban from all, and appoint other admins */}
1138           {this.canAdmin_ && (
1139             <>
1140               {!this.creatorIsAdmin_ && (
1141                 <>
1142                   {!isBanned(post_view.creator) ? (
1143                     <button
1144                       className="btn btn-link btn-animate text-muted py-0"
1145                       onClick={linkEvent(this, this.handleModBanShow)}
1146                       aria-label={i18n.t("ban_from_site")}
1147                     >
1148                       {i18n.t("ban_from_site")}
1149                     </button>
1150                   ) : (
1151                     <button
1152                       className="btn btn-link btn-animate text-muted py-0"
1153                       onClick={linkEvent(this, this.handleModBanSubmit)}
1154                       aria-label={i18n.t("unban_from_site")}
1155                     >
1156                       {this.state.banLoading ? (
1157                         <Spinner />
1158                       ) : (
1159                         i18n.t("unban_from_site")
1160                       )}
1161                     </button>
1162                   )}
1163                   <button
1164                     className="btn btn-link btn-animate text-muted py-0"
1165                     onClick={linkEvent(this, this.handlePurgePersonShow)}
1166                     aria-label={i18n.t("purge_user")}
1167                   >
1168                     {i18n.t("purge_user")}
1169                   </button>
1170                   <button
1171                     className="btn btn-link btn-animate text-muted py-0"
1172                     onClick={linkEvent(this, this.handlePurgePostShow)}
1173                     aria-label={i18n.t("purge_post")}
1174                   >
1175                     {i18n.t("purge_post")}
1176                   </button>
1177                 </>
1178               )}
1179               {!isBanned(post_view.creator) && post_view.creator.local && (
1180                 <button
1181                   className="btn btn-link btn-animate text-muted py-0"
1182                   onClick={linkEvent(this, this.handleAddAdmin)}
1183                   aria-label={
1184                     this.creatorIsAdmin_
1185                       ? i18n.t("remove_as_admin")
1186                       : i18n.t("appoint_as_admin")
1187                   }
1188                 >
1189                   {this.state.addAdminLoading ? (
1190                     <Spinner />
1191                   ) : this.creatorIsAdmin_ ? (
1192                     i18n.t("remove_as_admin")
1193                   ) : (
1194                     i18n.t("appoint_as_admin")
1195                   )}
1196                 </button>
1197               )}
1198             </>
1199           )}
1200         </>
1201       )
1202     );
1203   }
1204
1205   removeAndBanDialogs() {
1206     const post = this.postView;
1207     const purgeTypeText =
1208       this.state.purgeType == PurgeType.Post
1209         ? i18n.t("purge_post")
1210         : `${i18n.t("purge")} ${post.creator.name}`;
1211     return (
1212       <>
1213         {this.state.showRemoveDialog && (
1214           <form
1215             className="form-inline"
1216             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1217           >
1218             <label className="sr-only" htmlFor="post-listing-remove-reason">
1219               {i18n.t("reason")}
1220             </label>
1221             <input
1222               type="text"
1223               id="post-listing-remove-reason"
1224               className="form-control mr-2"
1225               placeholder={i18n.t("reason")}
1226               value={this.state.removeReason}
1227               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1228             />
1229             <button
1230               type="submit"
1231               className="btn btn-secondary"
1232               aria-label={i18n.t("remove_post")}
1233             >
1234               {this.state.removeLoading ? <Spinner /> : i18n.t("remove_post")}
1235             </button>
1236           </form>
1237         )}
1238         {this.state.showBanDialog && (
1239           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1240             <div className="form-group row col-12">
1241               <label
1242                 className="col-form-label"
1243                 htmlFor="post-listing-ban-reason"
1244               >
1245                 {i18n.t("reason")}
1246               </label>
1247               <input
1248                 type="text"
1249                 id="post-listing-ban-reason"
1250                 className="form-control mr-2"
1251                 placeholder={i18n.t("reason")}
1252                 value={this.state.banReason}
1253                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1254               />
1255               <label className="col-form-label" htmlFor={`mod-ban-expires`}>
1256                 {i18n.t("expires")}
1257               </label>
1258               <input
1259                 type="number"
1260                 id={`mod-ban-expires`}
1261                 className="form-control mr-2"
1262                 placeholder={i18n.t("number_of_days")}
1263                 value={this.state.banExpireDays}
1264                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1265               />
1266               <div className="form-group">
1267                 <div className="form-check">
1268                   <input
1269                     className="form-check-input"
1270                     id="mod-ban-remove-data"
1271                     type="checkbox"
1272                     checked={this.state.removeData}
1273                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1274                   />
1275                   <label
1276                     className="form-check-label"
1277                     htmlFor="mod-ban-remove-data"
1278                     title={i18n.t("remove_content_more")}
1279                   >
1280                     {i18n.t("remove_content")}
1281                   </label>
1282                 </div>
1283               </div>
1284             </div>
1285             {/* TODO hold off on expires until later */}
1286             {/* <div class="form-group row"> */}
1287             {/*   <label class="col-form-label">Expires</label> */}
1288             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1289             {/* </div> */}
1290             <div className="form-group row">
1291               <button
1292                 type="submit"
1293                 className="btn btn-secondary"
1294                 aria-label={i18n.t("ban")}
1295               >
1296                 {this.state.banLoading ? (
1297                   <Spinner />
1298                 ) : (
1299                   <span>
1300                     {i18n.t("ban")} {post.creator.name}
1301                   </span>
1302                 )}
1303               </button>
1304             </div>
1305           </form>
1306         )}
1307         {this.state.showReportDialog && (
1308           <form
1309             className="form-inline"
1310             onSubmit={linkEvent(this, this.handleReportSubmit)}
1311           >
1312             <label className="sr-only" htmlFor="post-report-reason">
1313               {i18n.t("reason")}
1314             </label>
1315             <input
1316               type="text"
1317               id="post-report-reason"
1318               className="form-control mr-2"
1319               placeholder={i18n.t("reason")}
1320               required
1321               value={this.state.reportReason}
1322               onInput={linkEvent(this, this.handleReportReasonChange)}
1323             />
1324             <button
1325               type="submit"
1326               className="btn btn-secondary"
1327               aria-label={i18n.t("create_report")}
1328             >
1329               {this.state.reportLoading ? <Spinner /> : i18n.t("create_report")}
1330             </button>
1331           </form>
1332         )}
1333         {this.state.showPurgeDialog && (
1334           <form
1335             className="form-inline"
1336             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1337           >
1338             <PurgeWarning />
1339             <label className="sr-only" htmlFor="purge-reason">
1340               {i18n.t("reason")}
1341             </label>
1342             <input
1343               type="text"
1344               id="purge-reason"
1345               className="form-control mr-2"
1346               placeholder={i18n.t("reason")}
1347               value={this.state.purgeReason}
1348               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1349             />
1350             {this.state.purgeLoading ? (
1351               <Spinner />
1352             ) : (
1353               <button
1354                 type="submit"
1355                 className="btn btn-secondary"
1356                 aria-label={purgeTypeText}
1357               >
1358                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1359               </button>
1360             )}
1361           </form>
1362         )}
1363       </>
1364     );
1365   }
1366
1367   mobileThumbnail() {
1368     const post = this.postView.post;
1369     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1370       <div className="row">
1371         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1372           {this.postTitleLine()}
1373         </div>
1374         <div className="col-4">
1375           {/* Post body prev or thumbnail */}
1376           {!this.state.imageExpanded && this.thumbnail()}
1377         </div>
1378       </div>
1379     ) : (
1380       this.postTitleLine()
1381     );
1382   }
1383
1384   showMobilePreview() {
1385     const { body, id } = this.postView.post;
1386
1387     return !this.showBody && body ? (
1388       <Link className="text-body" to={`/post/${id}`}>
1389         <div className="md-div mb-1 preview-lines">{body}</div>
1390       </Link>
1391     ) : (
1392       <></>
1393     );
1394   }
1395
1396   listing() {
1397     return (
1398       <>
1399         {/* The mobile view*/}
1400         <div className="d-block d-sm-none">
1401           <div className="row">
1402             <div className="col-12">
1403               {this.createdLine()}
1404
1405               {/* If it has a thumbnail, do a right aligned thumbnail */}
1406               {this.mobileThumbnail()}
1407
1408               {/* Show a preview of the post body */}
1409               {this.showMobilePreview()}
1410
1411               {this.commentsLine(true)}
1412               {this.userActionsLine()}
1413               {this.duplicatesLine()}
1414               {this.removeAndBanDialogs()}
1415             </div>
1416           </div>
1417         </div>
1418
1419         {/* The larger view*/}
1420         <div className="d-none d-sm-block">
1421           <div className="row">
1422             {!this.props.viewOnly && this.voteBar()}
1423             <div className="col-sm-2 pr-0">
1424               <div className="">{this.thumbnail()}</div>
1425             </div>
1426             <div className="col-12 col-sm-9">
1427               <div className="row">
1428                 <div className="col-12">
1429                   {this.postTitleLine()}
1430                   {this.createdLine()}
1431                   {this.commentsLine()}
1432                   {this.duplicatesLine()}
1433                   {this.userActionsLine()}
1434                   {this.removeAndBanDialogs()}
1435                 </div>
1436               </div>
1437             </div>
1438           </div>
1439         </div>
1440       </>
1441     );
1442   }
1443
1444   private get myPost(): boolean {
1445     return (
1446       this.postView.creator.id ==
1447       UserService.Instance.myUserInfo?.local_user_view.person.id
1448     );
1449   }
1450   handleEditClick(i: PostListing) {
1451     i.setState({ showEdit: true });
1452   }
1453
1454   handleEditCancel() {
1455     this.setState({ showEdit: false });
1456   }
1457
1458   // The actual editing is done in the receive for post
1459   handleEditPost(form: EditPost) {
1460     this.setState({ showEdit: false });
1461     this.props.onPostEdit(form);
1462   }
1463
1464   handleShare(i: PostListing) {
1465     const { name, body, id } = i.props.post_view.post;
1466     share({
1467       title: name,
1468       text: body?.slice(0, 50),
1469       url: `${getHttpBase()}/post/${id}`,
1470     });
1471   }
1472
1473   handleShowReportDialog(i: PostListing) {
1474     i.setState({ showReportDialog: !i.state.showReportDialog });
1475   }
1476
1477   handleReportReasonChange(i: PostListing, event: any) {
1478     i.setState({ reportReason: event.target.value });
1479   }
1480
1481   handleReportSubmit(i: PostListing, event: any) {
1482     event.preventDefault();
1483     i.setState({ reportLoading: true });
1484     i.props.onPostReport({
1485       post_id: i.postView.post.id,
1486       reason: i.state.reportReason ?? "",
1487       auth: myAuthRequired(),
1488     });
1489   }
1490
1491   handleBlockPersonClick(i: PostListing) {
1492     i.setState({ blockLoading: true });
1493     i.props.onBlockPerson({
1494       person_id: i.postView.creator.id,
1495       block: true,
1496       auth: myAuthRequired(),
1497     });
1498   }
1499
1500   handleDeleteClick(i: PostListing) {
1501     i.setState({ deleteLoading: true });
1502     i.props.onDeletePost({
1503       post_id: i.postView.post.id,
1504       deleted: !i.postView.post.deleted,
1505       auth: myAuthRequired(),
1506     });
1507   }
1508
1509   handleSavePostClick(i: PostListing) {
1510     i.setState({ saveLoading: true });
1511     i.props.onSavePost({
1512       post_id: i.postView.post.id,
1513       save: !i.postView.saved,
1514       auth: myAuthRequired(),
1515     });
1516   }
1517
1518   get crossPostParams(): PostFormParams {
1519     const queryParams: PostFormParams = {};
1520     const { name, url } = this.postView.post;
1521
1522     queryParams.name = name;
1523
1524     if (url) {
1525       queryParams.url = url;
1526     }
1527
1528     const crossPostBody = this.crossPostBody();
1529     if (crossPostBody) {
1530       queryParams.body = crossPostBody;
1531     }
1532
1533     return queryParams;
1534   }
1535
1536   crossPostBody(): string | undefined {
1537     const post = this.postView.post;
1538     const body = post.body;
1539
1540     return body
1541       ? `${i18n.t("cross_posted_from")} ${post.ap_id}\n\n${body.replace(
1542           /^/gm,
1543           "> "
1544         )}`
1545       : undefined;
1546   }
1547
1548   get showBody(): boolean {
1549     return this.props.showBody || this.state.showBody;
1550   }
1551
1552   handleModRemoveShow(i: PostListing) {
1553     i.setState({
1554       showRemoveDialog: !i.state.showRemoveDialog,
1555       showBanDialog: false,
1556     });
1557   }
1558
1559   handleModRemoveReasonChange(i: PostListing, event: any) {
1560     i.setState({ removeReason: event.target.value });
1561   }
1562
1563   handleModRemoveDataChange(i: PostListing, event: any) {
1564     i.setState({ removeData: event.target.checked });
1565   }
1566
1567   handleModRemoveSubmit(i: PostListing, event: any) {
1568     event.preventDefault();
1569     i.setState({ removeLoading: true });
1570     i.props.onRemovePost({
1571       post_id: i.postView.post.id,
1572       removed: !i.postView.post.removed,
1573       auth: myAuthRequired(),
1574     });
1575   }
1576
1577   handleModLock(i: PostListing) {
1578     i.setState({ lockLoading: true });
1579     i.props.onLockPost({
1580       post_id: i.postView.post.id,
1581       locked: !i.postView.post.locked,
1582       auth: myAuthRequired(),
1583     });
1584   }
1585
1586   handleModFeaturePostLocal(i: PostListing) {
1587     i.setState({ featureLocalLoading: true });
1588     i.props.onFeaturePost({
1589       post_id: i.postView.post.id,
1590       featured: !i.postView.post.featured_local,
1591       feature_type: "Local",
1592       auth: myAuthRequired(),
1593     });
1594   }
1595
1596   handleModFeaturePostCommunity(i: PostListing) {
1597     i.setState({ featureCommunityLoading: true });
1598     i.props.onFeaturePost({
1599       post_id: i.postView.post.id,
1600       featured: !i.postView.post.featured_community,
1601       feature_type: "Community",
1602       auth: myAuthRequired(),
1603     });
1604   }
1605
1606   handleModBanFromCommunityShow(i: PostListing) {
1607     i.setState({
1608       showBanDialog: true,
1609       banType: BanType.Community,
1610       showRemoveDialog: false,
1611     });
1612   }
1613
1614   handleModBanShow(i: PostListing) {
1615     i.setState({
1616       showBanDialog: true,
1617       banType: BanType.Site,
1618       showRemoveDialog: false,
1619     });
1620   }
1621
1622   handlePurgePersonShow(i: PostListing) {
1623     i.setState({
1624       showPurgeDialog: true,
1625       purgeType: PurgeType.Person,
1626       showRemoveDialog: false,
1627     });
1628   }
1629
1630   handlePurgePostShow(i: PostListing) {
1631     i.setState({
1632       showPurgeDialog: true,
1633       purgeType: PurgeType.Post,
1634       showRemoveDialog: false,
1635     });
1636   }
1637
1638   handlePurgeReasonChange(i: PostListing, event: any) {
1639     i.setState({ purgeReason: event.target.value });
1640   }
1641
1642   handlePurgeSubmit(i: PostListing, event: any) {
1643     event.preventDefault();
1644     i.setState({ purgeLoading: true });
1645     if (i.state.purgeType == PurgeType.Person) {
1646       i.props.onPurgePerson({
1647         person_id: i.postView.creator.id,
1648         reason: i.state.purgeReason,
1649         auth: myAuthRequired(),
1650       });
1651     } else if (i.state.purgeType == PurgeType.Post) {
1652       i.props.onPurgePost({
1653         post_id: i.postView.post.id,
1654         reason: i.state.purgeReason,
1655         auth: myAuthRequired(),
1656       });
1657     }
1658   }
1659
1660   handleModBanReasonChange(i: PostListing, event: any) {
1661     i.setState({ banReason: event.target.value });
1662   }
1663
1664   handleModBanExpireDaysChange(i: PostListing, event: any) {
1665     i.setState({ banExpireDays: event.target.value });
1666   }
1667
1668   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1669     i.setState({ banType: BanType.Community });
1670     i.handleModBanBothSubmit(i, event);
1671   }
1672
1673   handleModBanSubmit(i: PostListing, event: any) {
1674     i.setState({ banType: BanType.Site });
1675     i.handleModBanBothSubmit(i, event);
1676   }
1677
1678   handleModBanBothSubmit(i: PostListing, event: any) {
1679     event.preventDefault();
1680     i.setState({ banLoading: true });
1681
1682     const ban = !i.props.post_view.creator_banned_from_community;
1683     // If its an unban, restore all their data
1684     if (ban == false) {
1685       i.setState({ removeData: false });
1686     }
1687     const person_id = i.props.post_view.creator.id;
1688     const remove_data = i.state.removeData;
1689     const reason = i.state.banReason;
1690     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1691
1692     if (i.state.banType == BanType.Community) {
1693       const community_id = i.postView.community.id;
1694       i.props.onBanPersonFromCommunity({
1695         community_id,
1696         person_id,
1697         ban,
1698         remove_data,
1699         reason,
1700         expires,
1701         auth: myAuthRequired(),
1702       });
1703     } else {
1704       i.props.onBanPerson({
1705         person_id,
1706         ban,
1707         remove_data,
1708         reason,
1709         expires,
1710         auth: myAuthRequired(),
1711       });
1712     }
1713   }
1714
1715   handleAddModToCommunity(i: PostListing) {
1716     i.setState({ addModLoading: true });
1717     i.props.onAddModToCommunity({
1718       community_id: i.postView.community.id,
1719       person_id: i.postView.creator.id,
1720       added: !i.creatorIsMod_,
1721       auth: myAuthRequired(),
1722     });
1723   }
1724
1725   handleAddAdmin(i: PostListing) {
1726     i.setState({ addAdminLoading: true });
1727     i.props.onAddAdmin({
1728       person_id: i.postView.creator.id,
1729       added: !i.creatorIsAdmin_,
1730       auth: myAuthRequired(),
1731     });
1732   }
1733
1734   handleShowConfirmTransferCommunity(i: PostListing) {
1735     i.setState({ showConfirmTransferCommunity: true });
1736   }
1737
1738   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1739     i.setState({ showConfirmTransferCommunity: false });
1740   }
1741
1742   handleTransferCommunity(i: PostListing) {
1743     i.setState({ transferLoading: true });
1744     i.props.onTransferCommunity({
1745       community_id: i.postView.community.id,
1746       person_id: i.postView.creator.id,
1747       auth: myAuthRequired(),
1748     });
1749   }
1750
1751   handleShowConfirmTransferSite(i: PostListing) {
1752     i.setState({ showConfirmTransferSite: true });
1753   }
1754
1755   handleCancelShowConfirmTransferSite(i: PostListing) {
1756     i.setState({ showConfirmTransferSite: false });
1757   }
1758
1759   handleImageExpandClick(i: PostListing, event: any) {
1760     event.preventDefault();
1761     i.setState({ imageExpanded: !i.state.imageExpanded });
1762     setupTippy();
1763   }
1764
1765   handleViewSource(i: PostListing) {
1766     i.setState({ viewSource: !i.state.viewSource });
1767   }
1768
1769   handleShowAdvanced(i: PostListing) {
1770     i.setState({ showAdvanced: !i.state.showAdvanced });
1771     setupTippy();
1772   }
1773
1774   handleShowMoreMobile(i: PostListing) {
1775     i.setState({
1776       showMoreMobile: !i.state.showMoreMobile,
1777       showAdvanced: !i.state.showAdvanced,
1778     });
1779     setupTippy();
1780   }
1781
1782   handleShowBody(i: PostListing) {
1783     i.setState({ showBody: !i.state.showBody });
1784     setupTippy();
1785   }
1786
1787   handleUpvote(i: PostListing) {
1788     i.setState({ upvoteLoading: true });
1789     i.props.onPostVote({
1790       post_id: i.postView.post.id,
1791       score: newVote(VoteType.Upvote, i.props.post_view.my_vote),
1792       auth: myAuthRequired(),
1793     });
1794   }
1795
1796   handleDownvote(i: PostListing) {
1797     i.setState({ downvoteLoading: true });
1798     i.props.onPostVote({
1799       post_id: i.postView.post.id,
1800       score: newVote(VoteType.Downvote, i.props.post_view.my_vote),
1801       auth: myAuthRequired(),
1802     });
1803   }
1804
1805   get pointsTippy(): string {
1806     const points = i18n.t("number_of_points", {
1807       count: Number(this.postView.counts.score),
1808       formattedCount: Number(this.postView.counts.score),
1809     });
1810
1811     const upvotes = i18n.t("number_of_upvotes", {
1812       count: Number(this.postView.counts.upvotes),
1813       formattedCount: Number(this.postView.counts.upvotes),
1814     });
1815
1816     const downvotes = i18n.t("number_of_downvotes", {
1817       count: Number(this.postView.counts.downvotes),
1818       formattedCount: Number(this.postView.counts.downvotes),
1819     });
1820
1821     return `${points} • ${upvotes} • ${downvotes}`;
1822   }
1823
1824   get canModOnSelf_(): boolean {
1825     return canMod(
1826       this.postView.creator.id,
1827       this.props.moderators,
1828       this.props.admins,
1829       undefined,
1830       true
1831     );
1832   }
1833
1834   get canMod_(): boolean {
1835     return canMod(
1836       this.postView.creator.id,
1837       this.props.moderators,
1838       this.props.admins
1839     );
1840   }
1841
1842   get canAdmin_(): boolean {
1843     return canAdmin(this.postView.creator.id, this.props.admins);
1844   }
1845
1846   get creatorIsMod_(): boolean {
1847     return isMod(this.postView.creator.id, this.props.moderators);
1848   }
1849
1850   get creatorIsAdmin_(): boolean {
1851     return isAdmin(this.postView.creator.id, this.props.admins);
1852   }
1853 }