]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
fix: Remove wrapping li's
[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           </ul>
692         </div>
693       </>
694     );
695   }
696
697   get commentsButton() {
698     const post_view = this.postView;
699     const title = I18NextService.i18n.t("number_of_comments", {
700       count: Number(post_view.counts.comments),
701       formattedCount: Number(post_view.counts.comments),
702     });
703
704     return (
705       <Link
706         className="btn btn-link btn-sm text-muted ps-0"
707         title={title}
708         to={`/post/${post_view.post.id}?scrollToComments=true`}
709         data-tippy-content={title}
710       >
711         <Icon icon="message-square" classes="me-1" inline />
712         {post_view.counts.comments}
713         {this.unreadCount && (
714           <>
715             {" "}
716             <span className="fst-italic">
717               ({this.unreadCount} {I18NextService.i18n.t("new")})
718             </span>
719           </>
720         )}
721       </Link>
722     );
723   }
724
725   get unreadCount(): number | undefined {
726     const pv = this.postView;
727     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
728       ? undefined
729       : pv.unread_comments;
730   }
731
732   get saveButton() {
733     const saved = this.postView.saved;
734     const label = saved
735       ? I18NextService.i18n.t("unsave")
736       : I18NextService.i18n.t("save");
737     return (
738       <button
739         className="btn btn-sm btn-animate text-muted py-0"
740         onClick={linkEvent(this, this.handleSavePostClick)}
741         data-tippy-content={label}
742         aria-label={label}
743       >
744         {this.state.saveLoading ? (
745           <Spinner />
746         ) : (
747           <Icon
748             icon="star"
749             classes={classNames({ "text-warning": saved })}
750             inline
751           />
752         )}
753       </button>
754     );
755   }
756
757   get crossPostButton() {
758     return (
759       <Link
760         className="btn btn-sm btn-animate text-muted py-0"
761         to={{
762           /* Empty string properties are required to satisfy type*/
763           pathname: "/create_post",
764           state: { ...this.crossPostParams },
765           hash: "",
766           key: "",
767           search: "",
768         }}
769         title={I18NextService.i18n.t("cross_post")}
770         data-tippy-content={I18NextService.i18n.t("cross_post")}
771         aria-label={I18NextService.i18n.t("cross_post")}
772       >
773         <Icon icon="copy" inline />
774       </Link>
775     );
776   }
777
778   get reportButton() {
779     return (
780       <button
781         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
782         onClick={linkEvent(this, this.handleShowReportDialog)}
783         aria-label={I18NextService.i18n.t("show_report_dialog")}
784       >
785         <Icon classes="me-1" icon="flag" inline />
786         {I18NextService.i18n.t("create_report")}
787       </button>
788     );
789   }
790
791   get blockButton() {
792     return (
793       <button
794         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
795         onClick={linkEvent(this, this.handleBlockPersonClick)}
796         aria-label={I18NextService.i18n.t("block_user")}
797       >
798         {this.state.blockLoading ? (
799           <Spinner />
800         ) : (
801           <Icon classes="me-1" icon="slash" inline />
802         )}
803         {I18NextService.i18n.t("block_user")}
804       </button>
805     );
806   }
807
808   get editButton() {
809     return (
810       <button
811         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
812         onClick={linkEvent(this, this.handleEditClick)}
813         aria-label={I18NextService.i18n.t("edit")}
814       >
815         <Icon classes="me-1" icon="edit" inline />
816         {I18NextService.i18n.t("edit")}
817       </button>
818     );
819   }
820
821   get deleteButton() {
822     const deleted = this.postView.post.deleted;
823     const label = !deleted
824       ? I18NextService.i18n.t("delete")
825       : I18NextService.i18n.t("restore");
826     return (
827       <button
828         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
829         onClick={linkEvent(this, this.handleDeleteClick)}
830       >
831         {this.state.deleteLoading ? (
832           <Spinner />
833         ) : (
834           <>
835             <Icon
836               icon="trash"
837               classes={classNames("me-1", { "text-danger": deleted })}
838               inline
839             />
840             {label}
841           </>
842         )}
843       </button>
844     );
845   }
846
847   get viewSourceButton() {
848     return (
849       <button
850         className="btn btn-sm btn-animate text-muted py-0"
851         onClick={linkEvent(this, this.handleViewSource)}
852         data-tippy-content={I18NextService.i18n.t("view_source")}
853         aria-label={I18NextService.i18n.t("view_source")}
854       >
855         <Icon
856           icon="file-text"
857           classes={classNames({ "text-success": this.state.viewSource })}
858           inline
859         />
860       </button>
861     );
862   }
863
864   get lockButton() {
865     const locked = this.postView.post.locked;
866     const label = locked
867       ? I18NextService.i18n.t("unlock")
868       : I18NextService.i18n.t("lock");
869     return (
870       <button
871         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
872         onClick={linkEvent(this, this.handleModLock)}
873         aria-label={label}
874       >
875         {this.state.lockLoading ? (
876           <Spinner />
877         ) : (
878           <>
879             <Icon
880               icon="lock"
881               classes={classNames("me-1", { "text-danger": locked })}
882               inline
883             />
884             {capitalizeFirstLetter(label)}
885           </>
886         )}
887       </button>
888     );
889   }
890
891   get featureButtons() {
892     const featuredCommunity = this.postView.post.featured_community;
893     const labelCommunity = featuredCommunity
894       ? I18NextService.i18n.t("unfeature_from_community")
895       : I18NextService.i18n.t("feature_in_community");
896
897     const featuredLocal = this.postView.post.featured_local;
898     const labelLocal = featuredLocal
899       ? I18NextService.i18n.t("unfeature_from_local")
900       : I18NextService.i18n.t("feature_in_local");
901     return (
902       <>
903         <li>
904           <button
905             className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
906             onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
907             data-tippy-content={labelCommunity}
908             aria-label={labelCommunity}
909           >
910             {this.state.featureCommunityLoading ? (
911               <Spinner />
912             ) : (
913               <>
914                 <Icon
915                   icon="pin"
916                   classes={classNames("me-1", {
917                     "text-success": featuredCommunity,
918                   })}
919                   inline
920                 />
921                 {I18NextService.i18n.t("community")}
922               </>
923             )}
924           </button>
925         </li>
926         <li>
927           {amAdmin() && (
928             <button
929               className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
930               onClick={linkEvent(this, this.handleModFeaturePostLocal)}
931               data-tippy-content={labelLocal}
932               aria-label={labelLocal}
933             >
934               {this.state.featureLocalLoading ? (
935                 <Spinner />
936               ) : (
937                 <>
938                   <Icon
939                     icon="pin"
940                     classes={classNames("me-1", {
941                       "text-success": featuredLocal,
942                     })}
943                     inline
944                   />
945                   {I18NextService.i18n.t("local")}
946                 </>
947               )}
948             </button>
949           )}
950         </li>
951       </>
952     );
953   }
954
955   get modBanFromCommunityButton() {
956     return (
957       <button
958         className="btn btn-link btn-animate text-muted py-0"
959         onClick={linkEvent(this, this.handleModBanFromCommunityShow)}
960         aria-label={I18NextService.i18n.t("ban_from_community")}
961       >
962         {I18NextService.i18n.t("ban_from_community")}
963       </button>
964     );
965   }
966
967   get modUnbanFromCommunityButton() {
968     return (
969       <button
970         className="btn btn-link btn-animate text-muted py-0"
971         onClick={linkEvent(this, this.handleModBanFromCommunitySubmit)}
972         aria-label={I18NextService.i18n.t("unban")}
973       >
974         {this.state.banLoading ? <Spinner /> : I18NextService.i18n.t("unban")}
975       </button>
976     );
977   }
978
979   get addModToCommunityButton() {
980     return (
981       <button
982         className="btn btn-link btn-animate text-muted py-0"
983         onClick={linkEvent(this, this.handleAddModToCommunity)}
984         aria-label={
985           this.creatorIsMod_
986             ? I18NextService.i18n.t("remove_as_mod")
987             : I18NextService.i18n.t("appoint_as_mod")
988         }
989       >
990         {this.state.addModLoading ? (
991           <Spinner />
992         ) : this.creatorIsMod_ ? (
993           I18NextService.i18n.t("remove_as_mod")
994         ) : (
995           I18NextService.i18n.t("appoint_as_mod")
996         )}
997       </button>
998     );
999   }
1000
1001   get modBanButton() {
1002     return (
1003       <button
1004         className="btn btn-link btn-animate text-muted py-0"
1005         onClick={linkEvent(this, this.handleModBanShow)}
1006         aria-label={I18NextService.i18n.t("ban_from_site")}
1007       >
1008         {I18NextService.i18n.t("ban_from_site")}
1009       </button>
1010     );
1011   }
1012
1013   get modUnbanButton() {
1014     return (
1015       <button
1016         className="btn btn-link btn-animate text-muted py-0"
1017         onClick={linkEvent(this, this.handleModBanSubmit)}
1018         aria-label={I18NextService.i18n.t("unban_from_site")}
1019       >
1020         {this.state.banLoading ? (
1021           <Spinner />
1022         ) : (
1023           I18NextService.i18n.t("unban_from_site")
1024         )}
1025       </button>
1026     );
1027   }
1028
1029   get purgePersonButton() {
1030     return (
1031       <button
1032         className="btn btn-link btn-animate text-muted py-0"
1033         onClick={linkEvent(this, this.handlePurgePersonShow)}
1034         aria-label={I18NextService.i18n.t("purge_user")}
1035       >
1036         {I18NextService.i18n.t("purge_user")}
1037       </button>
1038     );
1039   }
1040
1041   get purgePostButton() {
1042     return (
1043       <button
1044         className="btn btn-link btn-animate text-muted py-0"
1045         onClick={linkEvent(this, this.handlePurgePostShow)}
1046         aria-label={I18NextService.i18n.t("purge_post")}
1047       >
1048         {I18NextService.i18n.t("purge_post")}
1049       </button>
1050     );
1051   }
1052
1053   get toggleAdminButton() {
1054     return (
1055       <button
1056         className="btn btn-link btn-animate text-muted py-0"
1057         onClick={linkEvent(this, this.handleAddAdmin)}
1058       >
1059         {this.state.addAdminLoading ? (
1060           <Spinner />
1061         ) : this.creatorIsAdmin_ ? (
1062           I18NextService.i18n.t("remove_as_admin")
1063         ) : (
1064           I18NextService.i18n.t("appoint_as_admin")
1065         )}
1066       </button>
1067     );
1068   }
1069
1070   get modRemoveButton() {
1071     const removed = this.postView.post.removed;
1072     return (
1073       <button
1074         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
1075         onClick={linkEvent(
1076           this,
1077           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
1078         )}
1079       >
1080         {/* TODO: Find an icon for this. */}
1081         {this.state.removeLoading ? (
1082           <Spinner />
1083         ) : !removed ? (
1084           I18NextService.i18n.t("remove")
1085         ) : (
1086           I18NextService.i18n.t("restore")
1087         )}
1088       </button>
1089     );
1090   }
1091
1092   /**
1093    * Mod/Admin actions to be taken against the author.
1094    */
1095   userActionsLine() {
1096     // TODO: make nicer
1097     const post_view = this.postView;
1098     return (
1099       this.state.showAdvanced && (
1100         <div className="mt-3 user-actions-line">
1101           {this.canMod_ && (
1102             <>
1103               {!this.creatorIsMod_ &&
1104                 (!post_view.creator_banned_from_community ? (
1105                   <li>{this.modBanFromCommunityButton}</li>
1106                 ) : (
1107                   <li>{this.modUnbanFromCommunityButton}</li>
1108                 ))}
1109               {!post_view.creator_banned_from_community && (
1110                 <li>{this.addModToCommunityButton}</li>
1111               )}
1112             </>
1113           )}
1114
1115           {/* Community creators and admins can transfer community to another mod */}
1116           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1117             this.canAdmin_) &&
1118             this.creatorIsMod_ &&
1119             (!this.state.showConfirmTransferCommunity ? (
1120               <button
1121                 className="btn btn-link btn-animate text-muted py-0 dropdown-item"
1122                 onClick={linkEvent(
1123                   this,
1124                   this.handleShowConfirmTransferCommunity
1125                 )}
1126                 aria-label={I18NextService.i18n.t("transfer_community")}
1127               >
1128                 {I18NextService.i18n.t("transfer_community")}
1129               </button>
1130             ) : (
1131               <>
1132                 <button
1133                   className="btn btn-link btn-animate text-muted py-0 dropdown-item"
1134                   aria-label={I18NextService.i18n.t("are_you_sure")}
1135                 >
1136                   {I18NextService.i18n.t("are_you_sure")}
1137                 </button>
1138                 <button
1139                   className="btn btn-link btn-animate text-muted py-0 dropdown-item"
1140                   aria-label={I18NextService.i18n.t("yes")}
1141                   onClick={linkEvent(this, this.handleTransferCommunity)}
1142                 >
1143                   {this.state.transferLoading ? (
1144                     <Spinner />
1145                   ) : (
1146                     I18NextService.i18n.t("yes")
1147                   )}
1148                 </button>
1149                 <button
1150                   className="btn btn-link btn-animate text-muted py-0 dropdown-item"
1151                   onClick={linkEvent(
1152                     this,
1153                     this.handleCancelShowConfirmTransferCommunity
1154                   )}
1155                   aria-label={I18NextService.i18n.t("no")}
1156                 >
1157                   {I18NextService.i18n.t("no")}
1158                 </button>
1159               </>
1160             ))}
1161           {/* Admins can ban from all, and appoint other admins */}
1162           {this.canAdmin_ && (
1163             <>
1164               {!this.creatorIsAdmin_ && (
1165                 <>
1166                   {!isBanned(post_view.creator)
1167                     ? this.modBanButton
1168                     : this.modUnbanButton}
1169                   {this.purgePersonButton}
1170                   {this.purgePostButton}
1171                 </>
1172               )}
1173               {!isBanned(post_view.creator) &&
1174                 post_view.creator.local &&
1175                 this.toggleAdminButton}
1176             </>
1177           )}
1178         </div>
1179       )
1180     );
1181   }
1182
1183   removeAndBanDialogs() {
1184     const post = this.postView;
1185     const purgeTypeText =
1186       this.state.purgeType == PurgeType.Post
1187         ? I18NextService.i18n.t("purge_post")
1188         : `${I18NextService.i18n.t("purge")} ${post.creator.name}`;
1189     return (
1190       <>
1191         {this.state.showRemoveDialog && (
1192           <form
1193             className="form-inline"
1194             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1195           >
1196             <label
1197               className="visually-hidden"
1198               htmlFor="post-listing-remove-reason"
1199             >
1200               {I18NextService.i18n.t("reason")}
1201             </label>
1202             <input
1203               type="text"
1204               id="post-listing-remove-reason"
1205               className="form-control me-2"
1206               placeholder={I18NextService.i18n.t("reason")}
1207               value={this.state.removeReason}
1208               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1209             />
1210             <button
1211               type="submit"
1212               className="btn btn-secondary"
1213               aria-label={I18NextService.i18n.t("remove_post")}
1214             >
1215               {this.state.removeLoading ? (
1216                 <Spinner />
1217               ) : (
1218                 I18NextService.i18n.t("remove_post")
1219               )}
1220             </button>
1221           </form>
1222         )}
1223         {this.state.showBanDialog && (
1224           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1225             <div className="mb-3 row col-12">
1226               <label
1227                 className="col-form-label"
1228                 htmlFor="post-listing-ban-reason"
1229               >
1230                 {I18NextService.i18n.t("reason")}
1231               </label>
1232               <input
1233                 type="text"
1234                 id="post-listing-ban-reason"
1235                 className="form-control me-2"
1236                 placeholder={I18NextService.i18n.t("reason")}
1237                 value={this.state.banReason}
1238                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1239               />
1240               <label className="col-form-label" htmlFor="mod-ban-expires">
1241                 {I18NextService.i18n.t("expires")}
1242               </label>
1243               <input
1244                 type="number"
1245                 id="mod-ban-expires"
1246                 className="form-control me-2"
1247                 placeholder={I18NextService.i18n.t("number_of_days")}
1248                 value={this.state.banExpireDays}
1249                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1250               />
1251               <div className="input-group mb-3">
1252                 <div className="form-check">
1253                   <input
1254                     className="form-check-input"
1255                     id="mod-ban-remove-data"
1256                     type="checkbox"
1257                     checked={this.state.removeData}
1258                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1259                   />
1260                   <label
1261                     className="form-check-label"
1262                     htmlFor="mod-ban-remove-data"
1263                     title={I18NextService.i18n.t("remove_content_more")}
1264                   >
1265                     {I18NextService.i18n.t("remove_content")}
1266                   </label>
1267                 </div>
1268               </div>
1269             </div>
1270             {/* TODO hold off on expires until later */}
1271             {/* <div class="mb-3 row"> */}
1272             {/*   <label class="col-form-label">Expires</label> */}
1273             {/*   <input type="date" class="form-control me-2" placeholder={I18NextService.i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1274             {/* </div> */}
1275             <div className="mb-3 row">
1276               <button
1277                 type="submit"
1278                 className="btn btn-secondary"
1279                 aria-label={I18NextService.i18n.t("ban")}
1280               >
1281                 {this.state.banLoading ? (
1282                   <Spinner />
1283                 ) : (
1284                   <span>
1285                     {I18NextService.i18n.t("ban")} {post.creator.name}
1286                   </span>
1287                 )}
1288               </button>
1289             </div>
1290           </form>
1291         )}
1292         {this.state.showReportDialog && (
1293           <form
1294             className="form-inline"
1295             onSubmit={linkEvent(this, this.handleReportSubmit)}
1296           >
1297             <label className="visually-hidden" htmlFor="post-report-reason">
1298               {I18NextService.i18n.t("reason")}
1299             </label>
1300             <input
1301               type="text"
1302               id="post-report-reason"
1303               className="form-control me-2"
1304               placeholder={I18NextService.i18n.t("reason")}
1305               required
1306               value={this.state.reportReason}
1307               onInput={linkEvent(this, this.handleReportReasonChange)}
1308             />
1309             <button
1310               type="submit"
1311               className="btn btn-secondary"
1312               aria-label={I18NextService.i18n.t("create_report")}
1313             >
1314               {this.state.reportLoading ? (
1315                 <Spinner />
1316               ) : (
1317                 I18NextService.i18n.t("create_report")
1318               )}
1319             </button>
1320           </form>
1321         )}
1322         {this.state.showPurgeDialog && (
1323           <form
1324             className="form-inline"
1325             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1326           >
1327             <PurgeWarning />
1328             <label className="visually-hidden" htmlFor="purge-reason">
1329               {I18NextService.i18n.t("reason")}
1330             </label>
1331             <input
1332               type="text"
1333               id="purge-reason"
1334               className="form-control me-2"
1335               placeholder={I18NextService.i18n.t("reason")}
1336               value={this.state.purgeReason}
1337               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1338             />
1339             {this.state.purgeLoading ? (
1340               <Spinner />
1341             ) : (
1342               <button
1343                 type="submit"
1344                 className="btn btn-secondary"
1345                 aria-label={purgeTypeText}
1346               >
1347                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1348               </button>
1349             )}
1350           </form>
1351         )}
1352       </>
1353     );
1354   }
1355
1356   mobileThumbnail() {
1357     const post = this.postView.post;
1358     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1359       <div className="row">
1360         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1361           {this.postTitleLine()}
1362         </div>
1363         <div className="col-4">
1364           {/* Post thumbnail */}
1365           {!this.state.imageExpanded && this.thumbnail()}
1366         </div>
1367       </div>
1368     ) : (
1369       this.postTitleLine()
1370     );
1371   }
1372
1373   listing() {
1374     return (
1375       <>
1376         {/* The mobile view*/}
1377         <div className="d-block d-sm-none">
1378           <article className="row post-container">
1379             <div className="col-12">
1380               {this.createdLine()}
1381
1382               {/* If it has a thumbnail, do a right aligned thumbnail */}
1383               {this.mobileThumbnail()}
1384
1385               {this.commentsLine(true)}
1386               {this.userActionsLine()}
1387               {this.duplicatesLine()}
1388               {this.removeAndBanDialogs()}
1389             </div>
1390           </article>
1391         </div>
1392
1393         {/* The larger view*/}
1394         <div className="d-none d-sm-block">
1395           <article className="row post-container">
1396             {!this.props.viewOnly && (
1397               <VoteButtons
1398                 voteContentType={VoteContentType.Post}
1399                 id={this.postView.post.id}
1400                 onVote={this.props.onPostVote}
1401                 enableDownvotes={this.props.enableDownvotes}
1402                 counts={this.postView.counts}
1403                 my_vote={this.postView.my_vote}
1404               />
1405             )}
1406             <div className="col-sm-2 pe-0 post-media">
1407               <div className="">{this.thumbnail()}</div>
1408             </div>
1409             <div className="col-12 col-sm-9">
1410               <div className="row">
1411                 <div className="col-12">
1412                   {this.postTitleLine()}
1413                   {this.createdLine()}
1414                   {this.commentsLine()}
1415                   {this.duplicatesLine()}
1416                   {this.userActionsLine()}
1417                   {this.removeAndBanDialogs()}
1418                 </div>
1419               </div>
1420             </div>
1421           </article>
1422         </div>
1423       </>
1424     );
1425   }
1426
1427   private get myPost(): boolean {
1428     return (
1429       this.postView.creator.id ==
1430       UserService.Instance.myUserInfo?.local_user_view.person.id
1431     );
1432   }
1433   handleEditClick(i: PostListing) {
1434     i.setState({ showEdit: true });
1435   }
1436
1437   handleEditCancel() {
1438     this.setState({ showEdit: false });
1439   }
1440
1441   // The actual editing is done in the receive for post
1442   handleEditPost(form: EditPost) {
1443     this.setState({ showEdit: false });
1444     this.props.onPostEdit(form);
1445   }
1446
1447   handleShare(i: PostListing) {
1448     const { name, body, id } = i.props.post_view.post;
1449     share({
1450       title: name,
1451       text: body?.slice(0, 50),
1452       url: `${getHttpBase()}/post/${id}`,
1453     });
1454   }
1455
1456   handleShowReportDialog(i: PostListing) {
1457     i.setState({ showReportDialog: !i.state.showReportDialog });
1458   }
1459
1460   handleReportReasonChange(i: PostListing, event: any) {
1461     i.setState({ reportReason: event.target.value });
1462   }
1463
1464   handleReportSubmit(i: PostListing, event: any) {
1465     event.preventDefault();
1466     i.setState({ reportLoading: true });
1467     i.props.onPostReport({
1468       post_id: i.postView.post.id,
1469       reason: i.state.reportReason ?? "",
1470       auth: myAuthRequired(),
1471     });
1472   }
1473
1474   handleBlockPersonClick(i: PostListing) {
1475     i.setState({ blockLoading: true });
1476     i.props.onBlockPerson({
1477       person_id: i.postView.creator.id,
1478       block: true,
1479       auth: myAuthRequired(),
1480     });
1481   }
1482
1483   handleDeleteClick(i: PostListing) {
1484     i.setState({ deleteLoading: true });
1485     i.props.onDeletePost({
1486       post_id: i.postView.post.id,
1487       deleted: !i.postView.post.deleted,
1488       auth: myAuthRequired(),
1489     });
1490   }
1491
1492   handleSavePostClick(i: PostListing) {
1493     i.setState({ saveLoading: true });
1494     i.props.onSavePost({
1495       post_id: i.postView.post.id,
1496       save: !i.postView.saved,
1497       auth: myAuthRequired(),
1498     });
1499   }
1500
1501   get crossPostParams(): PostFormParams {
1502     const queryParams: PostFormParams = {};
1503     const { name, url } = this.postView.post;
1504
1505     queryParams.name = name;
1506
1507     if (url) {
1508       queryParams.url = url;
1509     }
1510
1511     const crossPostBody = this.crossPostBody();
1512     if (crossPostBody) {
1513       queryParams.body = crossPostBody;
1514     }
1515
1516     return queryParams;
1517   }
1518
1519   crossPostBody(): string | undefined {
1520     const post = this.postView.post;
1521     const body = post.body;
1522
1523     return body
1524       ? `${I18NextService.i18n.t("cross_posted_from")} ${
1525           post.ap_id
1526         }\n\n${body.replace(/^/gm, "> ")}`
1527       : undefined;
1528   }
1529
1530   get showBody(): boolean {
1531     return this.props.showBody || this.state.showBody;
1532   }
1533
1534   handleModRemoveShow(i: PostListing) {
1535     i.setState({
1536       showRemoveDialog: !i.state.showRemoveDialog,
1537       showBanDialog: false,
1538     });
1539   }
1540
1541   handleModRemoveReasonChange(i: PostListing, event: any) {
1542     i.setState({ removeReason: event.target.value });
1543   }
1544
1545   handleModRemoveDataChange(i: PostListing, event: any) {
1546     i.setState({ removeData: event.target.checked });
1547   }
1548
1549   handleModRemoveSubmit(i: PostListing, event: any) {
1550     event.preventDefault();
1551     i.setState({ removeLoading: true });
1552     i.props.onRemovePost({
1553       post_id: i.postView.post.id,
1554       removed: !i.postView.post.removed,
1555       auth: myAuthRequired(),
1556     });
1557   }
1558
1559   handleModLock(i: PostListing) {
1560     i.setState({ lockLoading: true });
1561     i.props.onLockPost({
1562       post_id: i.postView.post.id,
1563       locked: !i.postView.post.locked,
1564       auth: myAuthRequired(),
1565     });
1566   }
1567
1568   handleModFeaturePostLocal(i: PostListing) {
1569     i.setState({ featureLocalLoading: true });
1570     i.props.onFeaturePost({
1571       post_id: i.postView.post.id,
1572       featured: !i.postView.post.featured_local,
1573       feature_type: "Local",
1574       auth: myAuthRequired(),
1575     });
1576   }
1577
1578   handleModFeaturePostCommunity(i: PostListing) {
1579     i.setState({ featureCommunityLoading: true });
1580     i.props.onFeaturePost({
1581       post_id: i.postView.post.id,
1582       featured: !i.postView.post.featured_community,
1583       feature_type: "Community",
1584       auth: myAuthRequired(),
1585     });
1586   }
1587
1588   handleModBanFromCommunityShow(i: PostListing) {
1589     i.setState({
1590       showBanDialog: true,
1591       banType: BanType.Community,
1592       showRemoveDialog: false,
1593     });
1594   }
1595
1596   handleModBanShow(i: PostListing) {
1597     i.setState({
1598       showBanDialog: true,
1599       banType: BanType.Site,
1600       showRemoveDialog: false,
1601     });
1602   }
1603
1604   handlePurgePersonShow(i: PostListing) {
1605     i.setState({
1606       showPurgeDialog: true,
1607       purgeType: PurgeType.Person,
1608       showRemoveDialog: false,
1609     });
1610   }
1611
1612   handlePurgePostShow(i: PostListing) {
1613     i.setState({
1614       showPurgeDialog: true,
1615       purgeType: PurgeType.Post,
1616       showRemoveDialog: false,
1617     });
1618   }
1619
1620   handlePurgeReasonChange(i: PostListing, event: any) {
1621     i.setState({ purgeReason: event.target.value });
1622   }
1623
1624   handlePurgeSubmit(i: PostListing, event: any) {
1625     event.preventDefault();
1626     i.setState({ purgeLoading: true });
1627     if (i.state.purgeType == PurgeType.Person) {
1628       i.props.onPurgePerson({
1629         person_id: i.postView.creator.id,
1630         reason: i.state.purgeReason,
1631         auth: myAuthRequired(),
1632       });
1633     } else if (i.state.purgeType == PurgeType.Post) {
1634       i.props.onPurgePost({
1635         post_id: i.postView.post.id,
1636         reason: i.state.purgeReason,
1637         auth: myAuthRequired(),
1638       });
1639     }
1640   }
1641
1642   handleModBanReasonChange(i: PostListing, event: any) {
1643     i.setState({ banReason: event.target.value });
1644   }
1645
1646   handleModBanExpireDaysChange(i: PostListing, event: any) {
1647     i.setState({ banExpireDays: event.target.value });
1648   }
1649
1650   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1651     i.setState({ banType: BanType.Community });
1652     i.handleModBanBothSubmit(i, event);
1653   }
1654
1655   handleModBanSubmit(i: PostListing, event: any) {
1656     i.setState({ banType: BanType.Site });
1657     i.handleModBanBothSubmit(i, event);
1658   }
1659
1660   handleModBanBothSubmit(i: PostListing, event: any) {
1661     event.preventDefault();
1662     i.setState({ banLoading: true });
1663
1664     const ban = !i.props.post_view.creator_banned_from_community;
1665     // If its an unban, restore all their data
1666     if (ban == false) {
1667       i.setState({ removeData: false });
1668     }
1669     const person_id = i.props.post_view.creator.id;
1670     const remove_data = i.state.removeData;
1671     const reason = i.state.banReason;
1672     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1673
1674     if (i.state.banType == BanType.Community) {
1675       const community_id = i.postView.community.id;
1676       i.props.onBanPersonFromCommunity({
1677         community_id,
1678         person_id,
1679         ban,
1680         remove_data,
1681         reason,
1682         expires,
1683         auth: myAuthRequired(),
1684       });
1685     } else {
1686       i.props.onBanPerson({
1687         person_id,
1688         ban,
1689         remove_data,
1690         reason,
1691         expires,
1692         auth: myAuthRequired(),
1693       });
1694     }
1695   }
1696
1697   handleAddModToCommunity(i: PostListing) {
1698     i.setState({ addModLoading: true });
1699     i.props.onAddModToCommunity({
1700       community_id: i.postView.community.id,
1701       person_id: i.postView.creator.id,
1702       added: !i.creatorIsMod_,
1703       auth: myAuthRequired(),
1704     });
1705   }
1706
1707   handleAddAdmin(i: PostListing) {
1708     i.setState({ addAdminLoading: true });
1709     i.props.onAddAdmin({
1710       person_id: i.postView.creator.id,
1711       added: !i.creatorIsAdmin_,
1712       auth: myAuthRequired(),
1713     });
1714   }
1715
1716   handleShowConfirmTransferCommunity(i: PostListing) {
1717     i.setState({ showConfirmTransferCommunity: true });
1718   }
1719
1720   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1721     i.setState({ showConfirmTransferCommunity: false });
1722   }
1723
1724   handleTransferCommunity(i: PostListing) {
1725     i.setState({ transferLoading: true });
1726     i.props.onTransferCommunity({
1727       community_id: i.postView.community.id,
1728       person_id: i.postView.creator.id,
1729       auth: myAuthRequired(),
1730     });
1731   }
1732
1733   handleShowConfirmTransferSite(i: PostListing) {
1734     i.setState({ showConfirmTransferSite: true });
1735   }
1736
1737   handleCancelShowConfirmTransferSite(i: PostListing) {
1738     i.setState({ showConfirmTransferSite: false });
1739   }
1740
1741   handleImageExpandClick(i: PostListing, event: any) {
1742     event.preventDefault();
1743     i.setState({ imageExpanded: !i.state.imageExpanded });
1744     setupTippy();
1745   }
1746
1747   handleViewSource(i: PostListing) {
1748     i.setState({ viewSource: !i.state.viewSource });
1749   }
1750
1751   handleShowAdvanced(i: PostListing) {
1752     i.setState({ showAdvanced: !i.state.showAdvanced });
1753     setupTippy();
1754   }
1755
1756   handleShowMoreMobile(i: PostListing) {
1757     i.setState({
1758       showMoreMobile: !i.state.showMoreMobile,
1759       showAdvanced: !i.state.showAdvanced,
1760     });
1761     setupTippy();
1762   }
1763
1764   handleShowBody(i: PostListing) {
1765     i.setState({ showBody: !i.state.showBody });
1766     setupTippy();
1767   }
1768
1769   get pointsTippy(): string {
1770     const points = I18NextService.i18n.t("number_of_points", {
1771       count: Number(this.postView.counts.score),
1772       formattedCount: Number(this.postView.counts.score),
1773     });
1774
1775     const upvotes = I18NextService.i18n.t("number_of_upvotes", {
1776       count: Number(this.postView.counts.upvotes),
1777       formattedCount: Number(this.postView.counts.upvotes),
1778     });
1779
1780     const downvotes = I18NextService.i18n.t("number_of_downvotes", {
1781       count: Number(this.postView.counts.downvotes),
1782       formattedCount: Number(this.postView.counts.downvotes),
1783     });
1784
1785     return `${points} • ${upvotes} • ${downvotes}`;
1786   }
1787
1788   get canModOnSelf_(): boolean {
1789     return canMod(
1790       this.postView.creator.id,
1791       this.props.moderators,
1792       this.props.admins,
1793       undefined,
1794       true
1795     );
1796   }
1797
1798   get canMod_(): boolean {
1799     return canMod(
1800       this.postView.creator.id,
1801       this.props.moderators,
1802       this.props.admins
1803     );
1804   }
1805
1806   get canAdmin_(): boolean {
1807     return canAdmin(this.postView.creator.id, this.props.admins);
1808   }
1809
1810   get creatorIsMod_(): boolean {
1811     return isMod(this.postView.creator.id, this.props.moderators);
1812   }
1813
1814   get creatorIsAdmin_(): boolean {
1815     return isAdmin(this.postView.creator.id, this.props.admins);
1816   }
1817 }