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