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