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