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