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