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