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