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