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