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