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