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