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