]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
Merge remote-tracking branch 'origin/main' into feat/vote-components
[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
660         {/**
661          * If there is a URL, or if the post has a body and we were told not to
662          * show the body, show the MetadataCard/body toggle.
663          */}
664         {(post.url || (post.body && !this.props.showBody)) &&
665           this.showPreviewButton()}
666
667         {this.showBody && post_view.post.body && this.viewSourceButton}
668
669         <div className="dropdown">
670           <button
671             className="btn btn-sm btn-animate text-muted py-0 dropdown-toggle"
672             onClick={linkEvent(this, this.handleShowAdvanced)}
673             data-tippy-content={I18NextService.i18n.t("more")}
674             data-bs-toggle="dropdown"
675             aria-expanded="false"
676             aria-controls="advancedButtonsDropdown"
677             aria-label={I18NextService.i18n.t("more")}
678           >
679             <Icon icon="more-vertical" inline />
680           </button>
681
682           <ul className="dropdown-menu" id="advancedButtonsDropdown">
683             <li>{this.crossPostButton}</li>
684             <li>
685               <hr className="dropdown-divider" />
686             </li>
687
688             {!this.myPost ? (
689               <>
690                 <li>{this.reportButton}</li>
691                 <li>{this.blockButton}</li>
692               </>
693             ) : (
694               <>
695                 <li>{this.editButton}</li>
696                 <li>{this.deleteButton}</li>
697               </>
698             )}
699
700             {/* Any mod can do these, not limited to hierarchy*/}
701             {(amMod(this.props.moderators) || amAdmin()) && (
702               <>
703                 <li>
704                   <hr className="dropdown-divider" />
705                 </li>
706                 <li>{this.lockButton}</li>
707                 {this.featureButtons}
708               </>
709             )}
710
711             {(this.canMod_ || this.canAdmin_) && (
712               <li>{this.modRemoveButton}</li>
713             )}
714           </ul>
715         </div>
716       </>
717     );
718   }
719
720   get commentsButton() {
721     const post_view = this.postView;
722     const title = I18NextService.i18n.t("number_of_comments", {
723       count: Number(post_view.counts.comments),
724       formattedCount: Number(post_view.counts.comments),
725     });
726
727     return (
728       <Link
729         className="btn btn-link btn-sm text-muted ps-0"
730         title={title}
731         to={`/post/${post_view.post.id}?scrollToComments=true`}
732         data-tippy-content={title}
733       >
734         <span className="me-1">
735           <Icon icon="message-square" classes="me-1" inline />
736           {post_view.counts.comments}
737         </span>
738         {this.unreadCount && (
739           <span className="text-muted fst-italic">
740             ({this.unreadCount} {I18NextService.i18n.t("new")})
741           </span>
742         )}
743       </Link>
744     );
745   }
746
747   get unreadCount(): number | undefined {
748     const pv = this.postView;
749     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
750       ? undefined
751       : pv.unread_comments;
752   }
753
754   get saveButton() {
755     const saved = this.postView.saved;
756     const label = saved
757       ? I18NextService.i18n.t("unsave")
758       : I18NextService.i18n.t("save");
759     return (
760       <button
761         className="btn btn-sm btn-animate text-muted py-0"
762         onClick={linkEvent(this, this.handleSavePostClick)}
763         data-tippy-content={label}
764         aria-label={label}
765       >
766         {this.state.saveLoading ? (
767           <Spinner />
768         ) : (
769           <Icon
770             icon="star"
771             classes={classNames({ "text-warning": saved })}
772             inline
773           />
774         )}
775       </button>
776     );
777   }
778
779   get crossPostButton() {
780     return (
781       <Link
782         className="btn btn-sm d-flex align-items-center rounded-0 dropdown-item"
783         to={{
784           /* Empty string properties are required to satisfy type*/
785           pathname: "/create_post",
786           state: { ...this.crossPostParams },
787           hash: "",
788           key: "",
789           search: "",
790         }}
791         title={I18NextService.i18n.t("cross_post")}
792         data-tippy-content={I18NextService.i18n.t("cross_post")}
793         aria-label={I18NextService.i18n.t("cross_post")}
794       >
795         <Icon classes="me-1" icon="copy" inline />
796         {I18NextService.i18n.t("cross_post")}
797       </Link>
798     );
799   }
800
801   get reportButton() {
802     return (
803       <button
804         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
805         onClick={linkEvent(this, this.handleShowReportDialog)}
806         aria-label={I18NextService.i18n.t("show_report_dialog")}
807       >
808         <Icon classes="me-1" icon="flag" inline />
809         {I18NextService.i18n.t("create_report")}
810       </button>
811     );
812   }
813
814   get blockButton() {
815     return (
816       <button
817         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
818         onClick={linkEvent(this, this.handleBlockPersonClick)}
819         aria-label={I18NextService.i18n.t("block_user")}
820       >
821         {this.state.blockLoading ? (
822           <Spinner />
823         ) : (
824           <Icon classes="me-1" icon="slash" inline />
825         )}
826         {I18NextService.i18n.t("block_user")}
827       </button>
828     );
829   }
830
831   get editButton() {
832     return (
833       <button
834         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
835         onClick={linkEvent(this, this.handleEditClick)}
836         aria-label={I18NextService.i18n.t("edit")}
837       >
838         <Icon classes="me-1" icon="edit" inline />
839         {I18NextService.i18n.t("edit")}
840       </button>
841     );
842   }
843
844   get deleteButton() {
845     const deleted = this.postView.post.deleted;
846     const label = !deleted
847       ? I18NextService.i18n.t("delete")
848       : I18NextService.i18n.t("restore");
849     return (
850       <button
851         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
852         onClick={linkEvent(this, this.handleDeleteClick)}
853       >
854         {this.state.deleteLoading ? (
855           <Spinner />
856         ) : (
857           <>
858             <Icon
859               icon="trash"
860               classes={classNames("me-1", { "text-danger": deleted })}
861               inline
862             />
863             {label}
864           </>
865         )}
866       </button>
867     );
868   }
869
870   get viewSourceButton() {
871     return (
872       <button
873         className="btn btn-sm btn-animate text-muted py-0"
874         onClick={linkEvent(this, this.handleViewSource)}
875         data-tippy-content={I18NextService.i18n.t("view_source")}
876         aria-label={I18NextService.i18n.t("view_source")}
877       >
878         <Icon
879           icon="file-text"
880           classes={classNames({ "text-success": this.state.viewSource })}
881           inline
882         />
883       </button>
884     );
885   }
886
887   get lockButton() {
888     const locked = this.postView.post.locked;
889     const label = locked
890       ? I18NextService.i18n.t("unlock")
891       : I18NextService.i18n.t("lock");
892     return (
893       <button
894         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
895         onClick={linkEvent(this, this.handleModLock)}
896         aria-label={label}
897       >
898         {this.state.lockLoading ? (
899           <Spinner />
900         ) : (
901           <>
902             <Icon
903               icon="lock"
904               classes={classNames("me-1", { "text-danger": locked })}
905               inline
906             />
907             {capitalizeFirstLetter(label)}
908           </>
909         )}
910       </button>
911     );
912   }
913
914   get featureButtons() {
915     const featuredCommunity = this.postView.post.featured_community;
916     const labelCommunity = featuredCommunity
917       ? I18NextService.i18n.t("unfeature_from_community")
918       : I18NextService.i18n.t("feature_in_community");
919
920     const featuredLocal = this.postView.post.featured_local;
921     const labelLocal = featuredLocal
922       ? I18NextService.i18n.t("unfeature_from_local")
923       : I18NextService.i18n.t("feature_in_local");
924     return (
925       <>
926         <li>
927           <button
928             className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
929             onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
930             data-tippy-content={labelCommunity}
931             aria-label={labelCommunity}
932           >
933             {this.state.featureCommunityLoading ? (
934               <Spinner />
935             ) : (
936               <>
937                 <Icon
938                   icon="pin"
939                   classes={classNames("me-1", {
940                     "text-success": featuredCommunity,
941                   })}
942                   inline
943                 />
944                 {I18NextService.i18n.t("community")}
945               </>
946             )}
947           </button>
948         </li>
949         <li>
950           {amAdmin() && (
951             <button
952               className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
953               onClick={linkEvent(this, this.handleModFeaturePostLocal)}
954               data-tippy-content={labelLocal}
955               aria-label={labelLocal}
956             >
957               {this.state.featureLocalLoading ? (
958                 <Spinner />
959               ) : (
960                 <>
961                   <Icon
962                     icon="pin"
963                     classes={classNames("me-1", {
964                       "text-success": featuredLocal,
965                     })}
966                     inline
967                   />
968                   {I18NextService.i18n.t("local")}
969                 </>
970               )}
971             </button>
972           )}
973         </li>
974       </>
975     );
976   }
977
978   get modRemoveButton() {
979     const removed = this.postView.post.removed;
980     return (
981       <button
982         className="btn btn-link btn-sm d-flex align-items-center rounded-0 dropdown-item"
983         onClick={linkEvent(
984           this,
985           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
986         )}
987       >
988         {/* TODO: Find an icon for this. */}
989         {this.state.removeLoading ? (
990           <Spinner />
991         ) : !removed ? (
992           I18NextService.i18n.t("remove")
993         ) : (
994           I18NextService.i18n.t("restore")
995         )}
996       </button>
997     );
998   }
999
1000   /**
1001    * Mod/Admin actions to be taken against the author.
1002    */
1003   userActionsLine() {
1004     // TODO: make nicer
1005     const post_view = this.postView;
1006     return (
1007       this.state.showAdvanced && (
1008         <>
1009           {this.canMod_ && (
1010             <>
1011               {!this.creatorIsMod_ &&
1012                 (!post_view.creator_banned_from_community ? (
1013                   <button
1014                     className="btn btn-link btn-animate text-muted py-0"
1015                     onClick={linkEvent(
1016                       this,
1017                       this.handleModBanFromCommunityShow
1018                     )}
1019                     aria-label={I18NextService.i18n.t("ban_from_community")}
1020                   >
1021                     {I18NextService.i18n.t("ban_from_community")}
1022                   </button>
1023                 ) : (
1024                   <button
1025                     className="btn btn-link btn-animate text-muted py-0"
1026                     onClick={linkEvent(
1027                       this,
1028                       this.handleModBanFromCommunitySubmit
1029                     )}
1030                     aria-label={I18NextService.i18n.t("unban")}
1031                   >
1032                     {this.state.banLoading ? (
1033                       <Spinner />
1034                     ) : (
1035                       I18NextService.i18n.t("unban")
1036                     )}
1037                   </button>
1038                 ))}
1039               {!post_view.creator_banned_from_community && (
1040                 <button
1041                   className="btn btn-link btn-animate text-muted py-0"
1042                   onClick={linkEvent(this, this.handleAddModToCommunity)}
1043                   aria-label={
1044                     this.creatorIsMod_
1045                       ? I18NextService.i18n.t("remove_as_mod")
1046                       : I18NextService.i18n.t("appoint_as_mod")
1047                   }
1048                 >
1049                   {this.state.addModLoading ? (
1050                     <Spinner />
1051                   ) : this.creatorIsMod_ ? (
1052                     I18NextService.i18n.t("remove_as_mod")
1053                   ) : (
1054                     I18NextService.i18n.t("appoint_as_mod")
1055                   )}
1056                 </button>
1057               )}
1058             </>
1059           )}
1060           {/* Community creators and admins can transfer community to another mod */}
1061           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1062             this.canAdmin_) &&
1063             this.creatorIsMod_ &&
1064             (!this.state.showConfirmTransferCommunity ? (
1065               <button
1066                 className="btn btn-link btn-animate text-muted py-0"
1067                 onClick={linkEvent(
1068                   this,
1069                   this.handleShowConfirmTransferCommunity
1070                 )}
1071                 aria-label={I18NextService.i18n.t("transfer_community")}
1072               >
1073                 {I18NextService.i18n.t("transfer_community")}
1074               </button>
1075             ) : (
1076               <>
1077                 <button
1078                   className="d-inline-block me-1 btn btn-link btn-animate text-muted py-0"
1079                   aria-label={I18NextService.i18n.t("are_you_sure")}
1080                 >
1081                   {I18NextService.i18n.t("are_you_sure")}
1082                 </button>
1083                 <button
1084                   className="btn btn-link btn-animate text-muted py-0 d-inline-block me-1"
1085                   aria-label={I18NextService.i18n.t("yes")}
1086                   onClick={linkEvent(this, this.handleTransferCommunity)}
1087                 >
1088                   {this.state.transferLoading ? (
1089                     <Spinner />
1090                   ) : (
1091                     I18NextService.i18n.t("yes")
1092                   )}
1093                 </button>
1094                 <button
1095                   className="btn btn-link btn-animate text-muted py-0 d-inline-block"
1096                   onClick={linkEvent(
1097                     this,
1098                     this.handleCancelShowConfirmTransferCommunity
1099                   )}
1100                   aria-label={I18NextService.i18n.t("no")}
1101                 >
1102                   {I18NextService.i18n.t("no")}
1103                 </button>
1104               </>
1105             ))}
1106           {/* Admins can ban from all, and appoint other admins */}
1107           {this.canAdmin_ && (
1108             <>
1109               {!this.creatorIsAdmin_ && (
1110                 <>
1111                   {!isBanned(post_view.creator) ? (
1112                     <button
1113                       className="btn btn-link btn-animate text-muted py-0"
1114                       onClick={linkEvent(this, this.handleModBanShow)}
1115                       aria-label={I18NextService.i18n.t("ban_from_site")}
1116                     >
1117                       {I18NextService.i18n.t("ban_from_site")}
1118                     </button>
1119                   ) : (
1120                     <button
1121                       className="btn btn-link btn-animate text-muted py-0"
1122                       onClick={linkEvent(this, this.handleModBanSubmit)}
1123                       aria-label={I18NextService.i18n.t("unban_from_site")}
1124                     >
1125                       {this.state.banLoading ? (
1126                         <Spinner />
1127                       ) : (
1128                         I18NextService.i18n.t("unban_from_site")
1129                       )}
1130                     </button>
1131                   )}
1132                   <button
1133                     className="btn btn-link btn-animate text-muted py-0"
1134                     onClick={linkEvent(this, this.handlePurgePersonShow)}
1135                     aria-label={I18NextService.i18n.t("purge_user")}
1136                   >
1137                     {I18NextService.i18n.t("purge_user")}
1138                   </button>
1139                   <button
1140                     className="btn btn-link btn-animate text-muted py-0"
1141                     onClick={linkEvent(this, this.handlePurgePostShow)}
1142                     aria-label={I18NextService.i18n.t("purge_post")}
1143                   >
1144                     {I18NextService.i18n.t("purge_post")}
1145                   </button>
1146                 </>
1147               )}
1148               {!isBanned(post_view.creator) && post_view.creator.local && (
1149                 <button
1150                   className="btn btn-link btn-animate text-muted py-0"
1151                   onClick={linkEvent(this, this.handleAddAdmin)}
1152                   aria-label={
1153                     this.creatorIsAdmin_
1154                       ? I18NextService.i18n.t("remove_as_admin")
1155                       : I18NextService.i18n.t("appoint_as_admin")
1156                   }
1157                 >
1158                   {this.state.addAdminLoading ? (
1159                     <Spinner />
1160                   ) : this.creatorIsAdmin_ ? (
1161                     I18NextService.i18n.t("remove_as_admin")
1162                   ) : (
1163                     I18NextService.i18n.t("appoint_as_admin")
1164                   )}
1165                 </button>
1166               )}
1167             </>
1168           )}
1169         </>
1170       )
1171     );
1172   }
1173
1174   removeAndBanDialogs() {
1175     const post = this.postView;
1176     const purgeTypeText =
1177       this.state.purgeType == PurgeType.Post
1178         ? I18NextService.i18n.t("purge_post")
1179         : `${I18NextService.i18n.t("purge")} ${post.creator.name}`;
1180     return (
1181       <>
1182         {this.state.showRemoveDialog && (
1183           <form
1184             className="form-inline"
1185             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1186           >
1187             <label
1188               className="visually-hidden"
1189               htmlFor="post-listing-remove-reason"
1190             >
1191               {I18NextService.i18n.t("reason")}
1192             </label>
1193             <input
1194               type="text"
1195               id="post-listing-remove-reason"
1196               className="form-control me-2"
1197               placeholder={I18NextService.i18n.t("reason")}
1198               value={this.state.removeReason}
1199               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1200             />
1201             <button
1202               type="submit"
1203               className="btn btn-secondary"
1204               aria-label={I18NextService.i18n.t("remove_post")}
1205             >
1206               {this.state.removeLoading ? (
1207                 <Spinner />
1208               ) : (
1209                 I18NextService.i18n.t("remove_post")
1210               )}
1211             </button>
1212           </form>
1213         )}
1214         {this.state.showBanDialog && (
1215           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1216             <div className="mb-3 row col-12">
1217               <label
1218                 className="col-form-label"
1219                 htmlFor="post-listing-ban-reason"
1220               >
1221                 {I18NextService.i18n.t("reason")}
1222               </label>
1223               <input
1224                 type="text"
1225                 id="post-listing-ban-reason"
1226                 className="form-control me-2"
1227                 placeholder={I18NextService.i18n.t("reason")}
1228                 value={this.state.banReason}
1229                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1230               />
1231               <label className="col-form-label" htmlFor={`mod-ban-expires`}>
1232                 {I18NextService.i18n.t("expires")}
1233               </label>
1234               <input
1235                 type="number"
1236                 id={`mod-ban-expires`}
1237                 className="form-control me-2"
1238                 placeholder={I18NextService.i18n.t("number_of_days")}
1239                 value={this.state.banExpireDays}
1240                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1241               />
1242               <div className="input-group mb-3">
1243                 <div className="form-check">
1244                   <input
1245                     className="form-check-input"
1246                     id="mod-ban-remove-data"
1247                     type="checkbox"
1248                     checked={this.state.removeData}
1249                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1250                   />
1251                   <label
1252                     className="form-check-label"
1253                     htmlFor="mod-ban-remove-data"
1254                     title={I18NextService.i18n.t("remove_content_more")}
1255                   >
1256                     {I18NextService.i18n.t("remove_content")}
1257                   </label>
1258                 </div>
1259               </div>
1260             </div>
1261             {/* TODO hold off on expires until later */}
1262             {/* <div class="mb-3 row"> */}
1263             {/*   <label class="col-form-label">Expires</label> */}
1264             {/*   <input type="date" class="form-control me-2" placeholder={I18NextService.i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1265             {/* </div> */}
1266             <div className="mb-3 row">
1267               <button
1268                 type="submit"
1269                 className="btn btn-secondary"
1270                 aria-label={I18NextService.i18n.t("ban")}
1271               >
1272                 {this.state.banLoading ? (
1273                   <Spinner />
1274                 ) : (
1275                   <span>
1276                     {I18NextService.i18n.t("ban")} {post.creator.name}
1277                   </span>
1278                 )}
1279               </button>
1280             </div>
1281           </form>
1282         )}
1283         {this.state.showReportDialog && (
1284           <form
1285             className="form-inline"
1286             onSubmit={linkEvent(this, this.handleReportSubmit)}
1287           >
1288             <label className="visually-hidden" htmlFor="post-report-reason">
1289               {I18NextService.i18n.t("reason")}
1290             </label>
1291             <input
1292               type="text"
1293               id="post-report-reason"
1294               className="form-control me-2"
1295               placeholder={I18NextService.i18n.t("reason")}
1296               required
1297               value={this.state.reportReason}
1298               onInput={linkEvent(this, this.handleReportReasonChange)}
1299             />
1300             <button
1301               type="submit"
1302               className="btn btn-secondary"
1303               aria-label={I18NextService.i18n.t("create_report")}
1304             >
1305               {this.state.reportLoading ? (
1306                 <Spinner />
1307               ) : (
1308                 I18NextService.i18n.t("create_report")
1309               )}
1310             </button>
1311           </form>
1312         )}
1313         {this.state.showPurgeDialog && (
1314           <form
1315             className="form-inline"
1316             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1317           >
1318             <PurgeWarning />
1319             <label className="visually-hidden" htmlFor="purge-reason">
1320               {I18NextService.i18n.t("reason")}
1321             </label>
1322             <input
1323               type="text"
1324               id="purge-reason"
1325               className="form-control me-2"
1326               placeholder={I18NextService.i18n.t("reason")}
1327               value={this.state.purgeReason}
1328               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1329             />
1330             {this.state.purgeLoading ? (
1331               <Spinner />
1332             ) : (
1333               <button
1334                 type="submit"
1335                 className="btn btn-secondary"
1336                 aria-label={purgeTypeText}
1337               >
1338                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1339               </button>
1340             )}
1341           </form>
1342         )}
1343       </>
1344     );
1345   }
1346
1347   mobileThumbnail() {
1348     const post = this.postView.post;
1349     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1350       <div className="row">
1351         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1352           {this.postTitleLine()}
1353         </div>
1354         <div className="col-4">
1355           {/* Post thumbnail */}
1356           {!this.state.imageExpanded && this.thumbnail()}
1357         </div>
1358       </div>
1359     ) : (
1360       this.postTitleLine()
1361     );
1362   }
1363
1364   bodyPreview() {
1365     const { body, id } = this.postView.post;
1366
1367     return !this.showBody && body ? (
1368       <Link className="text-body" to={`/post/${id}`}>
1369         <div className="md-div mb-1 preview-lines">{body}</div>
1370       </Link>
1371     ) : (
1372       <></>
1373     );
1374   }
1375
1376   listing() {
1377     return (
1378       <>
1379         {/* The mobile view*/}
1380         <div className="d-block d-sm-none">
1381           <article className="row post-container">
1382             <div className="col-12">
1383               {this.createdLine()}
1384
1385               {/* If it has a thumbnail, do a right aligned thumbnail */}
1386               {this.mobileThumbnail()}
1387
1388               <div className="mt-2">
1389                 {this.bodyPreview()}
1390                 {this.commentsLine(true)}
1391               </div>
1392               {this.userActionsLine()}
1393               {this.duplicatesLine()}
1394               {this.removeAndBanDialogs()}
1395             </div>
1396           </article>
1397         </div>
1398
1399         {/* The larger view*/}
1400         <div className="d-none d-sm-block">
1401           <article className="row post-container">
1402             {!this.props.viewOnly && (
1403               <VoteButtons
1404                 postListing={this}
1405                 enableDownvotes={this.props.enableDownvotes}
1406                 handleUpvote={this.handleUpvote}
1407                 handleDownvote={this.handleDownvote}
1408                 counts={this.postView.counts}
1409                 my_vote={this.postView.my_vote}
1410               />
1411             )}
1412             <div className="col-sm-2 pe-0 post-media">
1413               <div className="">{this.thumbnail()}</div>
1414             </div>
1415             <div className="col-12 col-sm-9">
1416               <div className="row">
1417                 <div className="col-12">
1418                   {this.postTitleLine()}
1419                   {this.createdLine()}
1420                   {this.bodyPreview()}
1421                   {this.commentsLine()}
1422                   {this.duplicatesLine()}
1423                   {this.userActionsLine()}
1424                   {this.removeAndBanDialogs()}
1425                 </div>
1426               </div>
1427             </div>
1428           </article>
1429         </div>
1430       </>
1431     );
1432   }
1433
1434   private get myPost(): boolean {
1435     return (
1436       this.postView.creator.id ==
1437       UserService.Instance.myUserInfo?.local_user_view.person.id
1438     );
1439   }
1440   handleEditClick(i: PostListing) {
1441     i.setState({ showEdit: true });
1442   }
1443
1444   handleEditCancel() {
1445     this.setState({ showEdit: false });
1446   }
1447
1448   // The actual editing is done in the receive for post
1449   handleEditPost(form: EditPost) {
1450     this.setState({ showEdit: false });
1451     this.props.onPostEdit(form);
1452   }
1453
1454   handleShare(i: PostListing) {
1455     const { name, body, id } = i.props.post_view.post;
1456     share({
1457       title: name,
1458       text: body?.slice(0, 50),
1459       url: `${getHttpBase()}/post/${id}`,
1460     });
1461   }
1462
1463   handleShowReportDialog(i: PostListing) {
1464     i.setState({ showReportDialog: !i.state.showReportDialog });
1465   }
1466
1467   handleReportReasonChange(i: PostListing, event: any) {
1468     i.setState({ reportReason: event.target.value });
1469   }
1470
1471   handleReportSubmit(i: PostListing, event: any) {
1472     event.preventDefault();
1473     i.setState({ reportLoading: true });
1474     i.props.onPostReport({
1475       post_id: i.postView.post.id,
1476       reason: i.state.reportReason ?? "",
1477       auth: myAuthRequired(),
1478     });
1479   }
1480
1481   handleBlockPersonClick(i: PostListing) {
1482     i.setState({ blockLoading: true });
1483     i.props.onBlockPerson({
1484       person_id: i.postView.creator.id,
1485       block: true,
1486       auth: myAuthRequired(),
1487     });
1488   }
1489
1490   handleDeleteClick(i: PostListing) {
1491     i.setState({ deleteLoading: true });
1492     i.props.onDeletePost({
1493       post_id: i.postView.post.id,
1494       deleted: !i.postView.post.deleted,
1495       auth: myAuthRequired(),
1496     });
1497   }
1498
1499   handleSavePostClick(i: PostListing) {
1500     i.setState({ saveLoading: true });
1501     i.props.onSavePost({
1502       post_id: i.postView.post.id,
1503       save: !i.postView.saved,
1504       auth: myAuthRequired(),
1505     });
1506   }
1507
1508   get crossPostParams(): PostFormParams {
1509     const queryParams: PostFormParams = {};
1510     const { name, url } = this.postView.post;
1511
1512     queryParams.name = name;
1513
1514     if (url) {
1515       queryParams.url = url;
1516     }
1517
1518     const crossPostBody = this.crossPostBody();
1519     if (crossPostBody) {
1520       queryParams.body = crossPostBody;
1521     }
1522
1523     return queryParams;
1524   }
1525
1526   crossPostBody(): string | undefined {
1527     const post = this.postView.post;
1528     const body = post.body;
1529
1530     return body
1531       ? `${I18NextService.i18n.t("cross_posted_from")} ${
1532           post.ap_id
1533         }\n\n${body.replace(/^/gm, "> ")}`
1534       : undefined;
1535   }
1536
1537   get showBody(): boolean {
1538     return this.props.showBody || this.state.showBody;
1539   }
1540
1541   handleModRemoveShow(i: PostListing) {
1542     i.setState({
1543       showRemoveDialog: !i.state.showRemoveDialog,
1544       showBanDialog: false,
1545     });
1546   }
1547
1548   handleModRemoveReasonChange(i: PostListing, event: any) {
1549     i.setState({ removeReason: event.target.value });
1550   }
1551
1552   handleModRemoveDataChange(i: PostListing, event: any) {
1553     i.setState({ removeData: event.target.checked });
1554   }
1555
1556   handleModRemoveSubmit(i: PostListing, event: any) {
1557     event.preventDefault();
1558     i.setState({ removeLoading: true });
1559     i.props.onRemovePost({
1560       post_id: i.postView.post.id,
1561       removed: !i.postView.post.removed,
1562       auth: myAuthRequired(),
1563     });
1564   }
1565
1566   handleModLock(i: PostListing) {
1567     i.setState({ lockLoading: true });
1568     i.props.onLockPost({
1569       post_id: i.postView.post.id,
1570       locked: !i.postView.post.locked,
1571       auth: myAuthRequired(),
1572     });
1573   }
1574
1575   handleModFeaturePostLocal(i: PostListing) {
1576     i.setState({ featureLocalLoading: true });
1577     i.props.onFeaturePost({
1578       post_id: i.postView.post.id,
1579       featured: !i.postView.post.featured_local,
1580       feature_type: "Local",
1581       auth: myAuthRequired(),
1582     });
1583   }
1584
1585   handleModFeaturePostCommunity(i: PostListing) {
1586     i.setState({ featureCommunityLoading: true });
1587     i.props.onFeaturePost({
1588       post_id: i.postView.post.id,
1589       featured: !i.postView.post.featured_community,
1590       feature_type: "Community",
1591       auth: myAuthRequired(),
1592     });
1593   }
1594
1595   handleModBanFromCommunityShow(i: PostListing) {
1596     i.setState({
1597       showBanDialog: true,
1598       banType: BanType.Community,
1599       showRemoveDialog: false,
1600     });
1601   }
1602
1603   handleModBanShow(i: PostListing) {
1604     i.setState({
1605       showBanDialog: true,
1606       banType: BanType.Site,
1607       showRemoveDialog: false,
1608     });
1609   }
1610
1611   handlePurgePersonShow(i: PostListing) {
1612     i.setState({
1613       showPurgeDialog: true,
1614       purgeType: PurgeType.Person,
1615       showRemoveDialog: false,
1616     });
1617   }
1618
1619   handlePurgePostShow(i: PostListing) {
1620     i.setState({
1621       showPurgeDialog: true,
1622       purgeType: PurgeType.Post,
1623       showRemoveDialog: false,
1624     });
1625   }
1626
1627   handlePurgeReasonChange(i: PostListing, event: any) {
1628     i.setState({ purgeReason: event.target.value });
1629   }
1630
1631   handlePurgeSubmit(i: PostListing, event: any) {
1632     event.preventDefault();
1633     i.setState({ purgeLoading: true });
1634     if (i.state.purgeType == PurgeType.Person) {
1635       i.props.onPurgePerson({
1636         person_id: i.postView.creator.id,
1637         reason: i.state.purgeReason,
1638         auth: myAuthRequired(),
1639       });
1640     } else if (i.state.purgeType == PurgeType.Post) {
1641       i.props.onPurgePost({
1642         post_id: i.postView.post.id,
1643         reason: i.state.purgeReason,
1644         auth: myAuthRequired(),
1645       });
1646     }
1647   }
1648
1649   handleModBanReasonChange(i: PostListing, event: any) {
1650     i.setState({ banReason: event.target.value });
1651   }
1652
1653   handleModBanExpireDaysChange(i: PostListing, event: any) {
1654     i.setState({ banExpireDays: event.target.value });
1655   }
1656
1657   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1658     i.setState({ banType: BanType.Community });
1659     i.handleModBanBothSubmit(i, event);
1660   }
1661
1662   handleModBanSubmit(i: PostListing, event: any) {
1663     i.setState({ banType: BanType.Site });
1664     i.handleModBanBothSubmit(i, event);
1665   }
1666
1667   handleModBanBothSubmit(i: PostListing, event: any) {
1668     event.preventDefault();
1669     i.setState({ banLoading: true });
1670
1671     const ban = !i.props.post_view.creator_banned_from_community;
1672     // If its an unban, restore all their data
1673     if (ban == false) {
1674       i.setState({ removeData: false });
1675     }
1676     const person_id = i.props.post_view.creator.id;
1677     const remove_data = i.state.removeData;
1678     const reason = i.state.banReason;
1679     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1680
1681     if (i.state.banType == BanType.Community) {
1682       const community_id = i.postView.community.id;
1683       i.props.onBanPersonFromCommunity({
1684         community_id,
1685         person_id,
1686         ban,
1687         remove_data,
1688         reason,
1689         expires,
1690         auth: myAuthRequired(),
1691       });
1692     } else {
1693       i.props.onBanPerson({
1694         person_id,
1695         ban,
1696         remove_data,
1697         reason,
1698         expires,
1699         auth: myAuthRequired(),
1700       });
1701     }
1702   }
1703
1704   handleAddModToCommunity(i: PostListing) {
1705     i.setState({ addModLoading: true });
1706     i.props.onAddModToCommunity({
1707       community_id: i.postView.community.id,
1708       person_id: i.postView.creator.id,
1709       added: !i.creatorIsMod_,
1710       auth: myAuthRequired(),
1711     });
1712   }
1713
1714   handleAddAdmin(i: PostListing) {
1715     i.setState({ addAdminLoading: true });
1716     i.props.onAddAdmin({
1717       person_id: i.postView.creator.id,
1718       added: !i.creatorIsAdmin_,
1719       auth: myAuthRequired(),
1720     });
1721   }
1722
1723   handleShowConfirmTransferCommunity(i: PostListing) {
1724     i.setState({ showConfirmTransferCommunity: true });
1725   }
1726
1727   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1728     i.setState({ showConfirmTransferCommunity: false });
1729   }
1730
1731   handleTransferCommunity(i: PostListing) {
1732     i.setState({ transferLoading: true });
1733     i.props.onTransferCommunity({
1734       community_id: i.postView.community.id,
1735       person_id: i.postView.creator.id,
1736       auth: myAuthRequired(),
1737     });
1738   }
1739
1740   handleShowConfirmTransferSite(i: PostListing) {
1741     i.setState({ showConfirmTransferSite: true });
1742   }
1743
1744   handleCancelShowConfirmTransferSite(i: PostListing) {
1745     i.setState({ showConfirmTransferSite: false });
1746   }
1747
1748   handleImageExpandClick(i: PostListing, event: any) {
1749     event.preventDefault();
1750     i.setState({ imageExpanded: !i.state.imageExpanded });
1751     setupTippy();
1752   }
1753
1754   handleViewSource(i: PostListing) {
1755     i.setState({ viewSource: !i.state.viewSource });
1756   }
1757
1758   handleShowAdvanced(i: PostListing) {
1759     i.setState({ showAdvanced: !i.state.showAdvanced });
1760     setupTippy();
1761   }
1762
1763   handleShowMoreMobile(i: PostListing) {
1764     i.setState({
1765       showMoreMobile: !i.state.showMoreMobile,
1766       showAdvanced: !i.state.showAdvanced,
1767     });
1768     setupTippy();
1769   }
1770
1771   handleShowBody(i: PostListing) {
1772     i.setState({ showBody: !i.state.showBody });
1773     setupTippy();
1774   }
1775
1776   handleUpvote(i: PostListing) {
1777     i.setState({ upvoteLoading: true });
1778     i.props.onPostVote({
1779       post_id: i.postView.post.id,
1780       score: newVote(VoteType.Upvote, i.props.post_view.my_vote),
1781       auth: myAuthRequired(),
1782     });
1783   }
1784
1785   handleDownvote(i: PostListing) {
1786     i.setState({ downvoteLoading: true });
1787     i.props.onPostVote({
1788       post_id: i.postView.post.id,
1789       score: newVote(VoteType.Downvote, i.props.post_view.my_vote),
1790       auth: myAuthRequired(),
1791     });
1792   }
1793
1794   get pointsTippy(): string {
1795     const points = I18NextService.i18n.t("number_of_points", {
1796       count: Number(this.postView.counts.score),
1797       formattedCount: Number(this.postView.counts.score),
1798     });
1799
1800     const upvotes = I18NextService.i18n.t("number_of_upvotes", {
1801       count: Number(this.postView.counts.upvotes),
1802       formattedCount: Number(this.postView.counts.upvotes),
1803     });
1804
1805     const downvotes = I18NextService.i18n.t("number_of_downvotes", {
1806       count: Number(this.postView.counts.downvotes),
1807       formattedCount: Number(this.postView.counts.downvotes),
1808     });
1809
1810     return `${points} • ${upvotes} • ${downvotes}`;
1811   }
1812
1813   get canModOnSelf_(): boolean {
1814     return canMod(
1815       this.postView.creator.id,
1816       this.props.moderators,
1817       this.props.admins,
1818       undefined,
1819       true
1820     );
1821   }
1822
1823   get canMod_(): boolean {
1824     return canMod(
1825       this.postView.creator.id,
1826       this.props.moderators,
1827       this.props.admins
1828     );
1829   }
1830
1831   get canAdmin_(): boolean {
1832     return canAdmin(this.postView.creator.id, this.props.admins);
1833   }
1834
1835   get creatorIsMod_(): boolean {
1836     return isMod(this.postView.creator.id, this.props.moderators);
1837   }
1838
1839   get creatorIsAdmin_(): boolean {
1840     return isAdmin(this.postView.creator.id, this.props.admins);
1841   }
1842 }