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