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