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