1 import classNames from "classnames";
2 import { Component, linkEvent } from "inferno";
3 import { Link } from "inferno-router";
10 CommunityModeratorView,
22 } from "lemmy-js-client";
23 import { externalHost } from "../../env";
24 import { i18n } from "../../i18next";
25 import { BanType } from "../../interfaces";
26 import { UserService, WebSocketService } from "../../services";
44 import { Icon } from "../common/icon";
45 import { MomentTime } from "../common/moment-time";
46 import { PictrsImage } from "../common/pictrs-image";
47 import { CommunityLink } from "../community/community-link";
48 import { PersonListing } from "../person/person-listing";
49 import { MetadataCard } from "./metadata-card";
50 import { PostForm } from "./post-form";
52 interface PostListingState {
54 showRemoveDialog: boolean;
56 showBanDialog: boolean;
59 banExpireDays: number;
61 showConfirmTransferSite: boolean;
62 showConfirmTransferCommunity: boolean;
63 imageExpanded: boolean;
65 showAdvanced: boolean;
66 showMoreMobile: boolean;
68 showReportDialog: boolean;
76 interface PostListingProps {
78 duplicates?: PostView[];
79 showCommunity?: boolean;
81 moderators?: CommunityModeratorView[];
82 admins?: PersonViewSafe[];
83 enableDownvotes: boolean;
87 export class PostListing extends Component<PostListingProps, PostListingState> {
88 private emptyState: PostListingState = {
90 showRemoveDialog: false,
96 banType: BanType.Community,
97 showConfirmTransferSite: false,
98 showConfirmTransferCommunity: false,
102 showMoreMobile: false,
104 showReportDialog: false,
106 my_vote: this.props.post_view.my_vote,
107 score: this.props.post_view.counts.score,
108 upvotes: this.props.post_view.counts.upvotes,
109 downvotes: this.props.post_view.counts.downvotes,
112 constructor(props: any, context: any) {
113 super(props, context);
115 this.state = this.emptyState;
116 this.handlePostLike = this.handlePostLike.bind(this);
117 this.handlePostDisLike = this.handlePostDisLike.bind(this);
118 this.handleEditPost = this.handleEditPost.bind(this);
119 this.handleEditCancel = this.handleEditCancel.bind(this);
122 componentWillReceiveProps(nextProps: PostListingProps) {
123 this.state.my_vote = nextProps.post_view.my_vote;
124 this.state.upvotes = nextProps.post_view.counts.upvotes;
125 this.state.downvotes = nextProps.post_view.counts.downvotes;
126 this.state.score = nextProps.post_view.counts.score;
127 if (this.props.post_view.post.id !== nextProps.post_view.post.id) {
128 this.state.imageExpanded = false;
130 this.setState(this.state);
134 let post = this.props.post_view.post;
136 <div class="post-listing">
137 {!this.state.showEdit ? (
140 {this.state.imageExpanded && this.img}
141 {post.url && this.showBody && post.embed_title && (
142 <MetadataCard post={post} />
144 {this.showBody && post.body && this.body()}
149 post_view={this.props.post_view}
150 onEdit={this.handleEditPost}
151 onCancel={this.handleEditCancel}
152 enableNsfw={this.props.enableNsfw}
153 enableDownvotes={this.props.enableDownvotes}
162 let post = this.props.post_view.post;
164 <div class="col-12 col-sm-9 offset-sm-3 card my-2 p-2">
165 {this.state.viewSource ? (
166 <pre>{post.body}</pre>
170 dangerouslySetInnerHTML={mdToHtml(post.body)}
180 <div class="offset-sm-3 my-2 d-none d-sm-block">
181 <a href={this.imageSrc} class="d-inline-block">
182 <PictrsImage src={this.imageSrc} />
185 <div className="my-2 d-block d-sm-none">
187 class="d-inline-block"
188 onClick={linkEvent(this, this.handleImageExpandClick)}
190 <PictrsImage src={this.imageSrc} />
197 imgThumb(src: string) {
198 let post_view = this.props.post_view;
204 nsfw={post_view.post.nsfw || post_view.community.nsfw}
209 get imageSrc(): string {
210 let post = this.props.post_view.post;
211 if (isImage(post.url)) {
212 if (post.url.includes("pictrs")) {
214 } else if (post.thumbnail_url) {
215 return post.thumbnail_url;
219 } else if (post.thumbnail_url) {
220 return post.thumbnail_url;
227 let post = this.props.post_view.post;
229 if (isImage(post.url)) {
233 class="text-body d-inline-block position-relative mb-2"
234 data-tippy-content={i18n.t("expand_here")}
235 onClick={linkEvent(this, this.handleImageExpandClick)}
236 aria-label={i18n.t("expand_here")}
238 {this.imgThumb(this.imageSrc)}
239 <Icon icon="image" classes="mini-overlay" />
242 } else if (post.thumbnail_url) {
245 class="text-body d-inline-block position-relative mb-2"
250 {this.imgThumb(this.imageSrc)}
251 <Icon icon="external-link" classes="mini-overlay" />
254 } else if (post.url) {
255 if (isVideo(post.url)) {
257 <div class="embed-responsive embed-responsive-16by9">
263 class="embed-responsive-item"
265 <source src={post.url} type="video/mp4" />
272 className="text-body"
277 <div class="thumbnail rounded bg-light d-flex justify-content-center">
278 <Icon icon="external-link" classes="d-flex align-items-center" />
286 className="text-body"
287 to={`/post/${post.id}`}
288 title={i18n.t("comments")}
290 <div class="thumbnail rounded bg-light d-flex justify-content-center">
291 <Icon icon="message-square" classes="d-flex align-items-center" />
299 let post_view = this.props.post_view;
301 <ul class="list-inline mb-1 text-muted small">
302 <li className="list-inline-item">
303 <PersonListing person={post_view.creator} />
305 {this.creatorIsMod && (
306 <span className="mx-1 badge badge-light">{i18n.t("mod")}</span>
308 {this.creatorIsAdmin && (
309 <span className="mx-1 badge badge-light">{i18n.t("admin")}</span>
311 {post_view.creator.bot_account && (
312 <span className="mx-1 badge badge-light">
313 {i18n.t("bot_account").toLowerCase()}
316 {(post_view.creator_banned_from_community ||
317 isBanned(post_view.creator)) && (
318 <span className="mx-1 badge badge-danger">{i18n.t("banned")}</span>
320 {post_view.creator_blocked && (
321 <span className="mx-1 badge badge-danger">{"blocked"}</span>
323 {this.props.showCommunity && (
325 <span class="mx-1"> {i18n.t("to")} </span>
326 <CommunityLink community={post_view.community} />
330 <li className="list-inline-item">•</li>
331 {post_view.post.url && !(hostname(post_view.post.url) == externalHost) && (
333 <li className="list-inline-item">
335 className="text-muted font-italic"
336 href={post_view.post.url}
337 title={post_view.post.url}
340 {hostname(post_view.post.url)}
343 <li className="list-inline-item">•</li>
346 <li className="list-inline-item">
348 <MomentTime data={post_view.post} />
351 {post_view.post.body && (
353 <li className="list-inline-item">•</li>
354 <li className="list-inline-item">
356 className="text-muted btn btn-sm btn-link p-0"
357 data-tippy-content={md.render(
358 previewLines(post_view.post.body)
360 data-tippy-allowHtml={true}
361 onClick={linkEvent(this, this.handleShowBody)}
363 <Icon icon="book-open" classes="icon-inline mr-1" />
374 <div className={`vote-bar col-1 pr-0 small text-center`}>
376 className={`btn-animate btn btn-link p-0 ${
377 this.state.my_vote == 1 ? "text-info" : "text-muted"
379 onClick={linkEvent(this, this.handlePostLike)}
380 data-tippy-content={i18n.t("upvote")}
381 aria-label={i18n.t("upvote")}
383 <Icon icon="arrow-up1" classes="upvote" />
387 class={`unselectable pointer font-weight-bold text-muted px-1`}
388 data-tippy-content={this.pointsTippy}
390 {numToSI(this.state.score)}
393 <div class="p-1"></div>
395 {this.props.enableDownvotes && (
397 className={`btn-animate btn btn-link p-0 ${
398 this.state.my_vote == -1 ? "text-danger" : "text-muted"
400 onClick={linkEvent(this, this.handlePostDisLike)}
401 data-tippy-content={i18n.t("downvote")}
402 aria-label={i18n.t("downvote")}
404 <Icon icon="arrow-down1" classes="downvote" />
412 let post = this.props.post_view.post;
414 <div className="post-title overflow-hidden">
416 {this.showBody && post.url ? (
418 className={!post.stickied ? "text-body" : "text-primary"}
427 className={!post.stickied ? "text-body" : "text-primary"}
428 to={`/post/${post.id}`}
429 title={i18n.t("comments")}
434 {(isImage(post.url) || post.thumbnail_url) && (
436 class="btn btn-link text-monospace text-muted small d-inline-block ml-2"
437 data-tippy-content={i18n.t("expand_here")}
438 onClick={linkEvent(this, this.handleImageExpandClick)}
442 !this.state.imageExpanded ? "plus-square" : "minus-square"
444 classes="icon-inline"
449 <small className="ml-2 text-muted font-italic">
455 className="unselectable pointer ml-2 text-muted font-italic"
456 data-tippy-content={i18n.t("deleted")}
458 <Icon icon="trash" classes="icon-inline text-danger" />
463 className="unselectable pointer ml-2 text-muted font-italic"
464 data-tippy-content={i18n.t("locked")}
466 <Icon icon="lock" classes="icon-inline text-danger" />
471 className="unselectable pointer ml-2 text-muted font-italic"
472 data-tippy-content={i18n.t("stickied")}
474 <Icon icon="pin" classes="icon-inline text-primary" />
478 <small className="ml-2 text-muted font-italic">
488 let dupes = this.props.duplicates;
491 dupes.length > 0 && (
492 <ul class="list-inline mb-1 small text-muted">
494 <li className="list-inline-item mr-2">
495 {i18n.t("cross_posted_to")}
498 <li className="list-inline-item mr-2">
499 <Link to={`/post/${pv.post.id}`}>
502 : `${pv.community.name}@${hostname(pv.community.actor_id)}`}
512 commentsLine(mobile = false) {
514 <div class="d-flex justify-content-start flex-wrap text-muted font-weight-bold mb-1">
515 {this.commentsButton}
516 {mobile && this.mobileVotes}
517 {UserService.Instance.myUserInfo && this.postActions(mobile)}
522 postActions(mobile = false) {
523 // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
524 // Possible enhancement: Make each button a component.
525 let post_view = this.props.post_view;
529 {this.crossPostButton}
530 {mobile && this.showMoreButton}
531 {(!mobile || this.state.showAdvanced) && (
539 {this.myPost && (this.showBody || this.state.showAdvanced) && (
547 {this.state.showAdvanced && (
549 {this.showBody && post_view.post.body && this.viewSourceButton}
550 {this.canModOnSelf && (
556 {(this.canMod || this.canAdmin || true) && (
557 <>{this.modRemoveButton}</>
561 {!mobile && this.showMoreButton}
566 get commentsButton() {
567 let post_view = this.props.post_view;
569 <button class="btn btn-link text-muted py-0 pl-0">
571 className="text-muted"
572 title={i18n.t("number_of_comments", {
573 count: post_view.counts.comments,
574 formattedCount: post_view.counts.comments,
576 to={`/post/${post_view.post.id}?scrollToComments=true`}
578 <Icon icon="message-square" classes="mr-1" inline />
579 {i18n.t("number_of_comments", {
580 count: post_view.counts.comments,
581 formattedCount: numToSI(post_view.counts.comments),
590 let tippy = showScores() ? { "data-tippy-content": this.pointsTippy } : {};
595 className={`btn-animate btn py-0 px-1 ${
596 this.state.my_vote == 1 ? "text-info" : "text-muted"
599 onClick={linkEvent(this, this.handlePostLike)}
600 aria-label={i18n.t("upvote")}
602 <Icon icon="arrow-up1" classes="icon-inline small" />
604 <span class="ml-2">{numToSI(this.state.upvotes)}</span>
607 {this.props.enableDownvotes && (
609 className={`ml-2 btn-animate btn py-0 px-1 ${
610 this.state.my_vote == -1 ? "text-danger" : "text-muted"
612 onClick={linkEvent(this, this.handlePostDisLike)}
614 aria-label={i18n.t("downvote")}
616 <Icon icon="arrow-down1" classes="icon-inline small" />
619 class={classNames("ml-2", {
620 invisible: this.state.downvotes === 0,
623 {numToSI(this.state.downvotes)}
634 let saved = this.props.post_view.saved;
635 let label = saved ? i18n.t("unsave") : i18n.t("save");
638 class="btn btn-link btn-animate text-muted py-0"
639 onClick={linkEvent(this, this.handleSavePostClick)}
640 data-tippy-content={label}
645 classes={classNames({ "text-warning": saved })}
652 get crossPostButton() {
655 className="btn btn-link btn-animate text-muted py-0"
656 to={`/create_post${this.crossPostParams}`}
657 title={i18n.t("cross_post")}
659 <Icon icon="copy" inline />
667 class="btn btn-link btn-animate text-muted py-0"
668 onClick={linkEvent(this, this.handleShowReportDialog)}
669 data-tippy-content={i18n.t("show_report_dialog")}
670 aria-label={i18n.t("show_report_dialog")}
672 <Icon icon="flag" inline />
680 class="btn btn-link btn-animate text-muted py-0"
681 onClick={linkEvent(this, this.handleBlockUserClick)}
682 data-tippy-content={i18n.t("block_user")}
683 aria-label={i18n.t("block_user")}
685 <Icon icon="slash" inline />
693 class="btn btn-link btn-animate text-muted py-0"
694 onClick={linkEvent(this, this.handleEditClick)}
695 data-tippy-content={i18n.t("edit")}
696 aria-label={i18n.t("edit")}
698 <Icon icon="edit" inline />
704 let deleted = this.props.post_view.post.deleted;
705 let label = !deleted ? i18n.t("delete") : i18n.t("restore");
708 class="btn btn-link btn-animate text-muted py-0"
709 onClick={linkEvent(this, this.handleDeleteClick)}
710 data-tippy-content={label}
715 classes={classNames({ "text-danger": deleted })}
722 get showMoreButton() {
725 class="btn btn-link btn-animate text-muted py-0"
726 onClick={linkEvent(this, this.handleShowAdvanced)}
727 data-tippy-content={i18n.t("more")}
728 aria-label={i18n.t("more")}
730 <Icon icon="more-vertical" inline />
735 get viewSourceButton() {
738 class="btn btn-link btn-animate text-muted py-0"
739 onClick={linkEvent(this, this.handleViewSource)}
740 data-tippy-content={i18n.t("view_source")}
741 aria-label={i18n.t("view_source")}
745 classes={classNames({ "text-success": this.state.viewSource })}
753 let locked = this.props.post_view.post.locked;
754 let label = locked ? i18n.t("unlock") : i18n.t("lock");
757 class="btn btn-link btn-animate text-muted py-0"
758 onClick={linkEvent(this, this.handleModLock)}
759 data-tippy-content={label}
764 classes={classNames({ "text-danger": locked })}
772 let stickied = this.props.post_view.post.stickied;
773 let label = stickied ? i18n.t("unsticky") : i18n.t("sticky");
776 class="btn btn-link btn-animate text-muted py-0"
777 onClick={linkEvent(this, this.handleModSticky)}
778 data-tippy-content={label}
783 classes={classNames({ "text-success": stickied })}
790 get modRemoveButton() {
791 let removed = this.props.post_view.post.removed;
794 class="btn btn-link btn-animate text-muted py-0"
797 !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
800 {/* TODO: Find an icon for this. */}
801 {!removed ? i18n.t("remove") : i18n.t("restore")}
807 * Mod/Admin actions to be taken against the author.
811 let post_view = this.props.post_view;
813 this.state.showAdvanced && (
817 {!this.creatorIsMod &&
818 (!post_view.creator_banned_from_community ? (
820 class="btn btn-link btn-animate text-muted py-0"
823 this.handleModBanFromCommunityShow
825 aria-label={i18n.t("ban")}
831 class="btn btn-link btn-animate text-muted py-0"
834 this.handleModBanFromCommunitySubmit
836 aria-label={i18n.t("unban")}
841 {!post_view.creator_banned_from_community && (
843 class="btn btn-link btn-animate text-muted py-0"
844 onClick={linkEvent(this, this.handleAddModToCommunity)}
847 ? i18n.t("remove_as_mod")
848 : i18n.t("appoint_as_mod")
852 ? i18n.t("remove_as_mod")
853 : i18n.t("appoint_as_mod")}
858 {/* Community creators and admins can transfer community to another mod */}
859 {(this.amCommunityCreator || this.canAdmin) &&
861 (!this.state.showConfirmTransferCommunity ? (
863 class="btn btn-link btn-animate text-muted py-0"
866 this.handleShowConfirmTransferCommunity
868 aria-label={i18n.t("transfer_community")}
870 {i18n.t("transfer_community")}
875 class="d-inline-block mr-1 btn btn-link btn-animate text-muted py-0"
876 aria-label={i18n.t("are_you_sure")}
878 {i18n.t("are_you_sure")}
881 class="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
882 aria-label={i18n.t("yes")}
883 onClick={linkEvent(this, this.handleTransferCommunity)}
888 class="btn btn-link btn-animate text-muted py-0 d-inline-block"
891 this.handleCancelShowConfirmTransferCommunity
893 aria-label={i18n.t("no")}
899 {/* Admins can ban from all, and appoint other admins */}
902 {!this.creatorIsAdmin &&
903 (!isBanned(post_view.creator) ? (
905 class="btn btn-link btn-animate text-muted py-0"
906 onClick={linkEvent(this, this.handleModBanShow)}
907 aria-label={i18n.t("ban_from_site")}
909 {i18n.t("ban_from_site")}
913 class="btn btn-link btn-animate text-muted py-0"
914 onClick={linkEvent(this, this.handleModBanSubmit)}
915 aria-label={i18n.t("unban_from_site")}
917 {i18n.t("unban_from_site")}
920 {!isBanned(post_view.creator) && post_view.creator.local && (
922 class="btn btn-link btn-animate text-muted py-0"
923 onClick={linkEvent(this, this.handleAddAdmin)}
926 ? i18n.t("remove_as_admin")
927 : i18n.t("appoint_as_admin")
931 ? i18n.t("remove_as_admin")
932 : i18n.t("appoint_as_admin")}
937 {/* Site Creator can transfer to another admin */}
938 {this.amSiteCreator &&
939 this.creatorIsAdmin &&
940 (!this.state.showConfirmTransferSite ? (
942 class="btn btn-link btn-animate text-muted py-0"
943 onClick={linkEvent(this, this.handleShowConfirmTransferSite)}
944 aria-label={i18n.t("transfer_site")}
946 {i18n.t("transfer_site")}
951 class="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
952 aria-label={i18n.t("are_you_sure")}
954 {i18n.t("are_you_sure")}
957 class="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
958 onClick={linkEvent(this, this.handleTransferSite)}
959 aria-label={i18n.t("yes")}
964 class="btn btn-link btn-animate text-muted py-0 d-inline-block"
967 this.handleCancelShowConfirmTransferSite
969 aria-label={i18n.t("no")}
980 removeAndBanDialogs() {
981 let post = this.props.post_view;
984 {this.state.showRemoveDialog && (
987 onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
989 <label class="sr-only" htmlFor="post-listing-remove-reason">
994 id="post-listing-remove-reason"
995 class="form-control mr-2"
996 placeholder={i18n.t("reason")}
997 value={this.state.removeReason}
998 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1002 class="btn btn-secondary"
1003 aria-label={i18n.t("remove_post")}
1005 {i18n.t("remove_post")}
1009 {this.state.showBanDialog && (
1010 <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1011 <div class="form-group row col-12">
1012 <label class="col-form-label" htmlFor="post-listing-ban-reason">
1017 id="post-listing-ban-reason"
1018 class="form-control mr-2"
1019 placeholder={i18n.t("reason")}
1020 value={this.state.banReason}
1021 onInput={linkEvent(this, this.handleModBanReasonChange)}
1023 <label class="col-form-label" htmlFor={`mod-ban-expires`}>
1028 id={`mod-ban-expires`}
1029 class="form-control mr-2"
1030 placeholder={i18n.t("number_of_days")}
1031 value={this.state.banExpireDays}
1032 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1034 <div class="form-group">
1035 <div class="form-check">
1037 class="form-check-input"
1038 id="mod-ban-remove-data"
1040 checked={this.state.removeData}
1041 onChange={linkEvent(this, this.handleModRemoveDataChange)}
1044 class="form-check-label"
1045 htmlFor="mod-ban-remove-data"
1046 title={i18n.t("remove_content_more")}
1048 {i18n.t("remove_content")}
1053 {/* TODO hold off on expires until later */}
1054 {/* <div class="form-group row"> */}
1055 {/* <label class="col-form-label">Expires</label> */}
1056 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1058 <div class="form-group row">
1061 class="btn btn-secondary"
1062 aria-label={i18n.t("ban")}
1064 {i18n.t("ban")} {post.creator.name}
1069 {this.state.showReportDialog && (
1072 onSubmit={linkEvent(this, this.handleReportSubmit)}
1074 <label class="sr-only" htmlFor="post-report-reason">
1079 id="post-report-reason"
1080 class="form-control mr-2"
1081 placeholder={i18n.t("reason")}
1083 value={this.state.reportReason}
1084 onInput={linkEvent(this, this.handleReportReasonChange)}
1088 class="btn btn-secondary"
1089 aria-label={i18n.t("create_report")}
1091 {i18n.t("create_report")}
1100 let post = this.props.post_view.post;
1101 return post.thumbnail_url || isImage(post.url) ? (
1103 <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1104 {this.postTitleLine()}
1107 {/* Post body prev or thumbnail */}
1108 {!this.state.imageExpanded && this.thumbnail()}
1112 this.postTitleLine()
1116 showMobilePreview() {
1117 let post = this.props.post_view.post;
1122 className="md-div mb-1"
1123 dangerouslySetInnerHTML={{
1124 __html: md.render(previewLines(post.body)),
1134 {/* The mobile view*/}
1135 <div class="d-block d-sm-none">
1137 <div class="col-12">
1138 {this.createdLine()}
1140 {/* If it has a thumbnail, do a right aligned thumbnail */}
1141 {this.mobileThumbnail()}
1143 {/* Show a preview of the post body */}
1144 {this.showMobilePreview()}
1146 {this.commentsLine(true)}
1147 {this.userActionsLine()}
1148 {this.duplicatesLine()}
1149 {this.removeAndBanDialogs()}
1154 {/* The larger view*/}
1155 <div class="d-none d-sm-block">
1158 <div class="col-sm-2 pr-0">
1159 <div class="">{this.thumbnail()}</div>
1161 <div class="col-12 col-sm-9">
1163 <div className="col-12">
1164 {this.postTitleLine()}
1165 {this.createdLine()}
1166 {this.commentsLine()}
1167 {this.duplicatesLine()}
1168 {this.userActionsLine()}
1169 {this.removeAndBanDialogs()}
1179 private get myPost(): boolean {
1181 UserService.Instance.myUserInfo &&
1182 this.props.post_view.creator.id ==
1183 UserService.Instance.myUserInfo.local_user_view.person.id
1187 get creatorIsMod(): boolean {
1189 this.props.moderators &&
1191 this.props.moderators.map(m => m.moderator.id),
1192 this.props.post_view.creator.id
1197 get creatorIsAdmin(): boolean {
1199 this.props.admins &&
1201 this.props.admins.map(a => a.person.id),
1202 this.props.post_view.creator.id
1208 * If the current user is allowed to mod this post.
1209 * The creator of this post is not allowed even if they are a mod.
1211 get canMod(): boolean {
1212 if (this.props.admins && this.props.moderators) {
1213 let adminsThenMods = this.props.admins
1214 .map(a => a.person.id)
1215 .concat(this.props.moderators.map(m => m.moderator.id));
1218 UserService.Instance.myUserInfo,
1220 this.props.post_view.creator.id
1228 * If the current user is allowed to mod this post.
1229 * The creator of this post is allowed if they are a mod.
1231 get canModOnSelf(): boolean {
1232 if (this.props.admins && this.props.moderators) {
1233 let adminsThenMods = this.props.admins
1234 .map(a => a.person.id)
1235 .concat(this.props.moderators.map(m => m.moderator.id));
1238 UserService.Instance.myUserInfo,
1240 this.props.post_view.creator.id,
1248 get canAdmin(): boolean {
1250 this.props.admins &&
1252 UserService.Instance.myUserInfo,
1253 this.props.admins.map(a => a.person.id),
1254 this.props.post_view.creator.id
1259 get amCommunityCreator(): boolean {
1261 this.props.moderators &&
1262 UserService.Instance.myUserInfo &&
1263 this.props.post_view.creator.id !=
1264 UserService.Instance.myUserInfo.local_user_view.person.id &&
1265 UserService.Instance.myUserInfo.local_user_view.person.id ==
1266 this.props.moderators[0].moderator.id
1270 get amSiteCreator(): boolean {
1272 this.props.admins &&
1273 UserService.Instance.myUserInfo &&
1274 this.props.post_view.creator.id !=
1275 UserService.Instance.myUserInfo.local_user_view.person.id &&
1276 UserService.Instance.myUserInfo.local_user_view.person.id ==
1277 this.props.admins[0].person.id
1281 handlePostLike(i: PostListing, event: any) {
1282 event.preventDefault();
1283 if (!UserService.Instance.myUserInfo) {
1284 this.context.router.history.push(`/login`);
1287 let new_vote = i.state.my_vote == 1 ? 0 : 1;
1289 if (i.state.my_vote == 1) {
1292 } else if (i.state.my_vote == -1) {
1293 i.state.downvotes--;
1301 i.state.my_vote = new_vote;
1303 let form: CreatePostLike = {
1304 post_id: i.props.post_view.post.id,
1305 score: i.state.my_vote,
1309 WebSocketService.Instance.send(wsClient.likePost(form));
1310 i.setState(i.state);
1314 handlePostDisLike(i: PostListing, event: any) {
1315 event.preventDefault();
1316 if (!UserService.Instance.myUserInfo) {
1317 this.context.router.history.push(`/login`);
1320 let new_vote = i.state.my_vote == -1 ? 0 : -1;
1322 if (i.state.my_vote == 1) {
1325 i.state.downvotes++;
1326 } else if (i.state.my_vote == -1) {
1327 i.state.downvotes--;
1330 i.state.downvotes++;
1334 i.state.my_vote = new_vote;
1336 let form: CreatePostLike = {
1337 post_id: i.props.post_view.post.id,
1338 score: i.state.my_vote,
1342 WebSocketService.Instance.send(wsClient.likePost(form));
1343 i.setState(i.state);
1347 handleEditClick(i: PostListing) {
1348 i.state.showEdit = true;
1349 i.setState(i.state);
1352 handleEditCancel() {
1353 this.state.showEdit = false;
1354 this.setState(this.state);
1357 // The actual editing is done in the recieve for post
1359 this.state.showEdit = false;
1360 this.setState(this.state);
1363 handleShowReportDialog(i: PostListing) {
1364 i.state.showReportDialog = !i.state.showReportDialog;
1365 i.setState(this.state);
1368 handleReportReasonChange(i: PostListing, event: any) {
1369 i.state.reportReason = event.target.value;
1370 i.setState(i.state);
1373 handleReportSubmit(i: PostListing, event: any) {
1374 event.preventDefault();
1375 let form: CreatePostReport = {
1376 post_id: i.props.post_view.post.id,
1377 reason: i.state.reportReason,
1380 WebSocketService.Instance.send(wsClient.createPostReport(form));
1382 i.state.showReportDialog = false;
1383 i.setState(i.state);
1386 handleBlockUserClick(i: PostListing) {
1387 let blockUserForm: BlockPerson = {
1388 person_id: i.props.post_view.creator.id,
1392 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
1395 handleDeleteClick(i: PostListing) {
1396 let deleteForm: DeletePost = {
1397 post_id: i.props.post_view.post.id,
1398 deleted: !i.props.post_view.post.deleted,
1401 WebSocketService.Instance.send(wsClient.deletePost(deleteForm));
1404 handleSavePostClick(i: PostListing) {
1406 i.props.post_view.saved == undefined ? true : !i.props.post_view.saved;
1407 let form: SavePost = {
1408 post_id: i.props.post_view.post.id,
1413 WebSocketService.Instance.send(wsClient.savePost(form));
1416 get crossPostParams(): string {
1417 let post = this.props.post_view.post;
1418 let params = `?title=${encodeURIComponent(post.name)}`;
1421 params += `&url=${encodeURIComponent(post.url)}`;
1424 params += `&body=${encodeURIComponent(this.crossPostBody())}`;
1429 crossPostBody(): string {
1430 let post = this.props.post_view.post;
1431 let body = `${i18n.t("cross_posted_from")} ${
1433 }\n\n${post.body.replace(/^/gm, "> ")}`;
1437 get showBody(): boolean {
1438 return this.props.showBody || this.state.showBody;
1441 handleModRemoveShow(i: PostListing) {
1442 i.state.showRemoveDialog = !i.state.showRemoveDialog;
1443 i.state.showBanDialog = false;
1444 i.setState(i.state);
1447 handleModRemoveReasonChange(i: PostListing, event: any) {
1448 i.state.removeReason = event.target.value;
1449 i.setState(i.state);
1452 handleModRemoveDataChange(i: PostListing, event: any) {
1453 i.state.removeData = event.target.checked;
1454 i.setState(i.state);
1457 handleModRemoveSubmit(i: PostListing, event: any) {
1458 event.preventDefault();
1459 let form: RemovePost = {
1460 post_id: i.props.post_view.post.id,
1461 removed: !i.props.post_view.post.removed,
1462 reason: i.state.removeReason,
1465 WebSocketService.Instance.send(wsClient.removePost(form));
1467 i.state.showRemoveDialog = false;
1468 i.setState(i.state);
1471 handleModLock(i: PostListing) {
1472 let form: LockPost = {
1473 post_id: i.props.post_view.post.id,
1474 locked: !i.props.post_view.post.locked,
1477 WebSocketService.Instance.send(wsClient.lockPost(form));
1480 handleModSticky(i: PostListing) {
1481 let form: StickyPost = {
1482 post_id: i.props.post_view.post.id,
1483 stickied: !i.props.post_view.post.stickied,
1486 WebSocketService.Instance.send(wsClient.stickyPost(form));
1489 handleModBanFromCommunityShow(i: PostListing) {
1490 i.state.showBanDialog = true;
1491 i.state.banType = BanType.Community;
1492 i.state.showRemoveDialog = false;
1493 i.setState(i.state);
1496 handleModBanShow(i: PostListing) {
1497 i.state.showBanDialog = true;
1498 i.state.banType = BanType.Site;
1499 i.state.showRemoveDialog = false;
1500 i.setState(i.state);
1503 handleModBanReasonChange(i: PostListing, event: any) {
1504 i.state.banReason = event.target.value;
1505 i.setState(i.state);
1508 handleModBanExpireDaysChange(i: PostListing, event: any) {
1509 i.state.banExpireDays = event.target.value;
1510 i.setState(i.state);
1513 handleModBanFromCommunitySubmit(i: PostListing) {
1514 i.state.banType = BanType.Community;
1515 i.setState(i.state);
1516 i.handleModBanBothSubmit(i);
1519 handleModBanSubmit(i: PostListing) {
1520 i.state.banType = BanType.Site;
1521 i.setState(i.state);
1522 i.handleModBanBothSubmit(i);
1525 handleModBanBothSubmit(i: PostListing, event?: any) {
1526 if (event) event.preventDefault();
1528 if (i.state.banType == BanType.Community) {
1529 // If its an unban, restore all their data
1530 let ban = !i.props.post_view.creator_banned_from_community;
1532 i.state.removeData = false;
1534 let form: BanFromCommunity = {
1535 person_id: i.props.post_view.creator.id,
1536 community_id: i.props.post_view.community.id,
1538 remove_data: i.state.removeData,
1539 reason: i.state.banReason,
1540 expires: futureDaysToUnixTime(i.state.banExpireDays),
1543 WebSocketService.Instance.send(wsClient.banFromCommunity(form));
1545 // If its an unban, restore all their data
1546 let ban = !i.props.post_view.creator.banned;
1548 i.state.removeData = false;
1550 let form: BanPerson = {
1551 person_id: i.props.post_view.creator.id,
1553 remove_data: i.state.removeData,
1554 reason: i.state.banReason,
1555 expires: futureDaysToUnixTime(i.state.banExpireDays),
1558 WebSocketService.Instance.send(wsClient.banPerson(form));
1561 i.state.showBanDialog = false;
1562 i.setState(i.state);
1565 handleAddModToCommunity(i: PostListing) {
1566 let form: AddModToCommunity = {
1567 person_id: i.props.post_view.creator.id,
1568 community_id: i.props.post_view.community.id,
1569 added: !i.creatorIsMod,
1572 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
1573 i.setState(i.state);
1576 handleAddAdmin(i: PostListing) {
1577 let form: AddAdmin = {
1578 person_id: i.props.post_view.creator.id,
1579 added: !i.creatorIsAdmin,
1582 WebSocketService.Instance.send(wsClient.addAdmin(form));
1583 i.setState(i.state);
1586 handleShowConfirmTransferCommunity(i: PostListing) {
1587 i.state.showConfirmTransferCommunity = true;
1588 i.setState(i.state);
1591 handleCancelShowConfirmTransferCommunity(i: PostListing) {
1592 i.state.showConfirmTransferCommunity = false;
1593 i.setState(i.state);
1596 handleTransferCommunity(i: PostListing) {
1597 let form: TransferCommunity = {
1598 community_id: i.props.post_view.community.id,
1599 person_id: i.props.post_view.creator.id,
1602 WebSocketService.Instance.send(wsClient.transferCommunity(form));
1603 i.state.showConfirmTransferCommunity = false;
1604 i.setState(i.state);
1607 handleShowConfirmTransferSite(i: PostListing) {
1608 i.state.showConfirmTransferSite = true;
1609 i.setState(i.state);
1612 handleCancelShowConfirmTransferSite(i: PostListing) {
1613 i.state.showConfirmTransferSite = false;
1614 i.setState(i.state);
1617 handleTransferSite(i: PostListing) {
1618 let form: TransferSite = {
1619 person_id: i.props.post_view.creator.id,
1622 WebSocketService.Instance.send(wsClient.transferSite(form));
1623 i.state.showConfirmTransferSite = false;
1624 i.setState(i.state);
1627 handleImageExpandClick(i: PostListing, event: any) {
1628 event.preventDefault();
1629 i.state.imageExpanded = !i.state.imageExpanded;
1630 i.state.showBody = i.state.imageExpanded;
1631 i.setState(i.state);
1635 handleViewSource(i: PostListing) {
1636 i.state.viewSource = !i.state.viewSource;
1637 i.setState(i.state);
1640 handleShowAdvanced(i: PostListing) {
1641 i.state.showAdvanced = !i.state.showAdvanced;
1642 i.setState(i.state);
1646 handleShowMoreMobile(i: PostListing) {
1647 i.state.showMoreMobile = !i.state.showMoreMobile;
1648 i.state.showAdvanced = !i.state.showAdvanced;
1649 i.setState(i.state);
1653 handleShowBody(i: PostListing) {
1654 i.state.showBody = !i.state.showBody;
1655 i.setState(i.state);
1659 get pointsTippy(): string {
1660 let points = i18n.t("number_of_points", {
1661 count: this.state.score,
1662 formattedCount: this.state.score,
1665 let upvotes = i18n.t("number_of_upvotes", {
1666 count: this.state.upvotes,
1667 formattedCount: this.state.upvotes,
1670 let downvotes = i18n.t("number_of_downvotes", {
1671 count: this.state.downvotes,
1672 formattedCount: this.state.downvotes,
1675 return `${points} • ${upvotes} • ${downvotes}`;