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