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