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