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