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