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