1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
11 GetPersonDetailsResponse,
19 } from "lemmy-js-client";
20 import moment from "moment";
21 import { Subscription } from "rxjs";
22 import { i18n } from "../../i18next";
23 import { InitialFetchRequest, PersonDetailsView } from "../../interfaces";
24 import { UserService, WebSocketService } from "../../services";
27 capitalizeFirstLetter,
29 createPostLikeFindRes,
43 restoreScrollPosition,
54 import { BannerIconHeader } from "../common/banner-icon-header";
55 import { HtmlTags } from "../common/html-tags";
56 import { Icon, Spinner } from "../common/icon";
57 import { MomentTime } from "../common/moment-time";
58 import { SortSelect } from "../common/sort-select";
59 import { CommunityLink } from "../community/community-link";
60 import { PersonDetails } from "./person-details";
61 import { PersonListing } from "./person-listing";
63 interface ProfileState {
64 personRes?: GetPersonDetailsResponse;
66 view: PersonDetailsView;
70 personBlocked: boolean;
72 banExpireDays?: number;
73 showBanDialog: boolean;
75 siteRes: GetSiteResponse;
78 interface ProfileProps {
79 view: PersonDetailsView;
92 export class Profile extends Component<any, ProfileState> {
93 private isoData = setIsoData(this.context);
94 private subscription?: Subscription;
95 state: ProfileState = {
96 userName: getUsernameFromProps(this.props),
98 view: Profile.getViewFromProps(this.props.match.view),
99 sort: Profile.getSortTypeFromProps(this.props.match.sort),
100 page: Profile.getPageFromProps(this.props.match.page),
101 personBlocked: false,
102 siteRes: this.isoData.site_res,
103 showBanDialog: false,
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.handleSortChange = this.handleSortChange.bind(this);
111 this.handlePageChange = this.handlePageChange.bind(this);
113 this.parseMessage = this.parseMessage.bind(this);
114 this.subscription = wsSubscribe(this.parseMessage);
116 // Only fetch the data if coming from another route
117 if (this.isoData.path == this.context.router.route.match.url) {
120 personRes: this.isoData.routeData[0] as GetPersonDetailsResponse,
124 this.fetchUserData();
129 let form: GetPersonDetails = {
130 username: this.state.userName,
131 sort: this.state.sort,
132 saved_only: this.state.view === PersonDetailsView.Saved,
133 page: this.state.page,
137 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
140 get amCurrentUser() {
142 UserService.Instance.myUserInfo?.local_user_view.person.id ==
143 this.state.personRes?.person_view.person.id
148 let mui = UserService.Instance.myUserInfo;
149 let res = this.state.personRes;
152 personBlocked: mui.person_blocks
153 .map(a => a.target.id)
154 .includes(res.person_view.person.id),
159 static getViewFromProps(view: string): PersonDetailsView {
160 return view ? PersonDetailsView[view] : PersonDetailsView.Overview;
163 static getSortTypeFromProps(sort: string): SortType {
164 return sort ? routeSortTypeToEnum(sort) : SortType.New;
167 static getPageFromProps(page: number): number {
168 return page ? Number(page) : 1;
171 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
172 let pathSplit = req.path.split("/");
174 let username = pathSplit[2];
175 let view = this.getViewFromProps(pathSplit[4]);
176 let sort = this.getSortTypeFromProps(pathSplit[6]);
177 let page = this.getPageFromProps(Number(pathSplit[8]));
179 let form: GetPersonDetails = {
182 saved_only: view === PersonDetailsView.Saved,
187 return [req.client.getPersonDetails(form)];
190 componentDidMount() {
191 this.setPersonBlock();
195 componentWillUnmount() {
196 this.subscription?.unsubscribe();
197 saveScrollPosition(this.context);
200 static getDerivedStateFromProps(props: any): ProfileProps {
202 view: this.getViewFromProps(props.match.params.view),
203 sort: this.getSortTypeFromProps(props.match.params.sort),
204 page: this.getPageFromProps(props.match.params.page),
205 person_id: Number(props.match.params.id),
206 username: props.match.params.username,
210 componentDidUpdate(lastProps: any) {
211 // Necessary if you are on a post and you click another post (same route)
213 lastProps.location.pathname.split("/")[2] !==
214 lastProps.history.location.pathname.split("/")[2]
216 // Couldnt get a refresh working. This does for now.
221 get documentTitle(): string {
222 let res = this.state.personRes;
224 ? `@${res.person_view.person.name} - ${this.state.siteRes.site_view.site.name}`
229 let res = this.state.personRes;
231 <div className="container-lg">
232 {this.state.loading ? (
238 <div className="row">
239 <div className="col-12 col-md-8">
242 title={this.documentTitle}
243 path={this.context.router.route.match.url}
244 description={res.person_view.person.bio}
245 image={res.person_view.person.avatar}
250 {!this.state.loading && this.selects()}
253 admins={this.state.siteRes.admins}
254 sort={this.state.sort}
255 page={this.state.page}
257 enableDownvotes={enableDownvotes(this.state.siteRes)}
258 enableNsfw={enableNsfw(this.state.siteRes)}
259 view={this.state.view}
260 onPageChange={this.handlePageChange}
261 allLanguages={this.state.siteRes.all_languages}
262 siteLanguages={this.state.siteRes.discussion_languages}
266 {!this.state.loading && (
267 <div className="col-12 col-md-4">
269 {this.amCurrentUser && this.follows()}
281 <div className="btn-group btn-group-toggle flex-wrap mb-2">
283 className={`btn btn-outline-secondary pointer
284 ${this.state.view == PersonDetailsView.Overview && "active"}
289 value={PersonDetailsView.Overview}
290 checked={this.state.view === PersonDetailsView.Overview}
291 onChange={linkEvent(this, this.handleViewChange)}
296 className={`btn btn-outline-secondary pointer
297 ${this.state.view == PersonDetailsView.Comments && "active"}
302 value={PersonDetailsView.Comments}
303 checked={this.state.view == PersonDetailsView.Comments}
304 onChange={linkEvent(this, this.handleViewChange)}
309 className={`btn btn-outline-secondary pointer
310 ${this.state.view == PersonDetailsView.Posts && "active"}
315 value={PersonDetailsView.Posts}
316 checked={this.state.view == PersonDetailsView.Posts}
317 onChange={linkEvent(this, this.handleViewChange)}
322 className={`btn btn-outline-secondary pointer
323 ${this.state.view == PersonDetailsView.Saved && "active"}
328 value={PersonDetailsView.Saved}
329 checked={this.state.view == PersonDetailsView.Saved}
330 onChange={linkEvent(this, this.handleViewChange)}
339 let profileRss = `/feeds/u/${this.state.userName}.xml?sort=${this.state.sort}`;
342 <div className="mb-2">
343 <span className="mr-3">{this.viewRadios()}</span>
345 sort={this.state.sort}
346 onChange={this.handleSortChange}
350 <a href={profileRss} rel={relTags} title="RSS">
351 <Icon icon="rss" classes="text-muted small mx-2" />
353 <link rel="alternate" type="application/atom+xml" href={profileRss} />
357 handleBlockPerson(personId: number) {
361 let blockUserForm: BlockPerson = {
366 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
370 handleUnblockPerson(recipientId: number) {
373 let blockUserForm: BlockPerson = {
374 person_id: recipientId,
378 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
383 let pv = this.state.personRes?.person_view;
387 <BannerIconHeader banner={pv.person.banner} icon={pv.person.avatar} />
388 <div className="mb-3">
390 <div className="mb-0 d-flex flex-wrap">
392 {pv.person.display_name && (
393 <h5 className="mb-0">{pv.person.display_name}</h5>
395 <ul className="list-inline mb-2">
396 <li className="list-inline-item">
405 {isBanned(pv.person) && (
406 <li className="list-inline-item badge badge-danger">
410 {pv.person.deleted && (
411 <li className="list-inline-item badge badge-danger">
415 {pv.person.admin && (
416 <li className="list-inline-item badge badge-light">
420 {pv.person.bot_account && (
421 <li className="list-inline-item badge badge-light">
422 {i18n.t("bot_account").toLowerCase()}
428 <div className="flex-grow-1 unselectable pointer mx-2"></div>
429 {!this.amCurrentUser && UserService.Instance.myUserInfo && (
432 className={`d-flex align-self-start btn btn-secondary mr-2 ${
433 !pv.person.matrix_user_id && "invisible"
436 href={`https://matrix.to/#/${pv.person.matrix_user_id}`}
438 {i18n.t("send_secure_message")}
442 "d-flex align-self-start btn btn-secondary mr-2"
444 to={`/create_private_message/recipient/${pv.person.id}`}
446 {i18n.t("send_message")}
448 {this.state.personBlocked ? (
451 "d-flex align-self-start btn btn-secondary mr-2"
455 this.handleUnblockPerson
458 {i18n.t("unblock_user")}
463 "d-flex align-self-start btn btn-secondary mr-2"
467 this.handleBlockPerson
470 {i18n.t("block_user")}
476 {canMod(pv.person.id, undefined, this.state.siteRes.admins) &&
477 !isAdmin(pv.person.id, this.state.siteRes.admins) &&
478 !this.state.showBanDialog &&
479 (!isBanned(pv.person) ? (
482 "d-flex align-self-start btn btn-secondary mr-2"
484 onClick={linkEvent(this, this.handleModBanShow)}
485 aria-label={i18n.t("ban")}
487 {capitalizeFirstLetter(i18n.t("ban"))}
492 "d-flex align-self-start btn btn-secondary mr-2"
494 onClick={linkEvent(this, this.handleModBanSubmit)}
495 aria-label={i18n.t("unban")}
497 {capitalizeFirstLetter(i18n.t("unban"))}
502 <div className="d-flex align-items-center mb-2">
505 dangerouslySetInnerHTML={mdToHtml(pv.person.bio)}
510 <ul className="list-inline mb-2">
511 <li className="list-inline-item badge badge-light">
512 {i18n.t("number_of_posts", {
513 count: pv.counts.post_count,
514 formattedCount: numToSI(pv.counts.post_count),
517 <li className="list-inline-item badge badge-light">
518 {i18n.t("number_of_comments", {
519 count: pv.counts.comment_count,
520 formattedCount: numToSI(pv.counts.comment_count),
525 <div className="text-muted">
526 {i18n.t("joined")}{" "}
528 published={pv.person.published}
533 <div className="d-flex align-items-center text-muted mb-2">
535 <span className="ml-2">
536 {i18n.t("cake_day_title")}{" "}
538 .utc(pv.person.published)
540 .format("MMM DD, YYYY")}
551 let pv = this.state.personRes?.person_view;
555 {this.state.showBanDialog && (
556 <form onSubmit={linkEvent(this, this.handleModBanSubmit)}>
557 <div className="form-group row col-12">
558 <label className="col-form-label" htmlFor="profile-ban-reason">
563 id="profile-ban-reason"
564 className="form-control mr-2"
565 placeholder={i18n.t("reason")}
566 value={this.state.banReason}
567 onInput={linkEvent(this, this.handleModBanReasonChange)}
569 <label className="col-form-label" htmlFor={`mod-ban-expires`}>
574 id={`mod-ban-expires`}
575 className="form-control mr-2"
576 placeholder={i18n.t("number_of_days")}
577 value={this.state.banExpireDays}
578 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
580 <div className="form-group">
581 <div className="form-check">
583 className="form-check-input"
584 id="mod-ban-remove-data"
586 checked={this.state.removeData}
587 onChange={linkEvent(this, this.handleModRemoveDataChange)}
590 className="form-check-label"
591 htmlFor="mod-ban-remove-data"
592 title={i18n.t("remove_content_more")}
594 {i18n.t("remove_content")}
599 {/* TODO hold off on expires until later */}
600 {/* <div class="form-group row"> */}
601 {/* <label class="col-form-label">Expires</label> */}
602 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
604 <div className="form-group row">
607 className="btn btn-secondary mr-2"
608 aria-label={i18n.t("cancel")}
609 onClick={linkEvent(this, this.handleModBanSubmitCancel)}
615 className="btn btn-secondary"
616 aria-label={i18n.t("ban")}
618 {i18n.t("ban")} {pv.person.name}
629 let moderates = this.state.personRes?.moderates;
632 moderates.length > 0 && (
633 <div className="card border-secondary mb-3">
634 <div className="card-body">
635 <h5>{i18n.t("moderates")}</h5>
636 <ul className="list-unstyled mb-0">
637 {moderates.map(cmv => (
638 <li key={cmv.community.id}>
639 <CommunityLink community={cmv.community} />
650 let follows = UserService.Instance.myUserInfo?.follows;
653 follows.length > 0 && (
654 <div className="card border-secondary mb-3">
655 <div className="card-body">
656 <h5>{i18n.t("subscribed")}</h5>
657 <ul className="list-unstyled mb-0">
658 {follows.map(cfv => (
659 <li key={cfv.community.id}>
660 <CommunityLink community={cfv.community} />
670 updateUrl(paramUpdates: UrlParams) {
671 const page = paramUpdates.page || this.state.page;
672 const viewStr = paramUpdates.view || PersonDetailsView[this.state.view];
673 const sortStr = paramUpdates.sort || this.state.sort;
675 let typeView = `/u/${this.state.userName}`;
677 this.props.history.push(
678 `${typeView}/view/${viewStr}/sort/${sortStr}/page/${page}`
680 this.setState({ loading: true });
681 this.fetchUserData();
684 handlePageChange(page: number) {
685 this.updateUrl({ page: page });
688 handleSortChange(val: SortType) {
689 this.updateUrl({ sort: val, page: 1 });
692 handleViewChange(i: Profile, event: any) {
694 view: PersonDetailsView[Number(event.target.value)],
699 handleModBanShow(i: Profile) {
700 i.setState({ showBanDialog: true });
703 handleModBanReasonChange(i: Profile, event: any) {
704 i.setState({ banReason: event.target.value });
707 handleModBanExpireDaysChange(i: Profile, event: any) {
708 i.setState({ banExpireDays: event.target.value });
711 handleModRemoveDataChange(i: Profile, event: any) {
712 i.setState({ removeData: event.target.checked });
715 handleModBanSubmitCancel(i: Profile, event?: any) {
716 event.preventDefault();
717 i.setState({ showBanDialog: false });
720 handleModBanSubmit(i: Profile, event?: any) {
721 if (event) event.preventDefault();
722 let person = i.state.personRes?.person_view.person;
724 if (person && auth) {
725 // If its an unban, restore all their data
726 let ban = !person.banned;
728 i.setState({ removeData: false });
730 let form: BanPerson = {
731 person_id: person.id,
733 remove_data: i.state.removeData,
734 reason: i.state.banReason,
735 expires: futureDaysToUnixTime(i.state.banExpireDays),
738 WebSocketService.Instance.send(wsClient.banPerson(form));
740 i.setState({ showBanDialog: false });
744 parseMessage(msg: any) {
745 let op = wsUserOp(msg);
748 toast(i18n.t(msg.error), "danger");
749 if (msg.error == "couldnt_find_that_username_or_email") {
750 this.context.router.history.push("/");
753 } else if (msg.reconnect) {
754 this.fetchUserData();
755 } else if (op == UserOperation.GetPersonDetails) {
756 // Since the PersonDetails contains posts/comments as well as some general user info we listen here as well
757 // and set the parent state if it is not set or differs
758 // TODO this might need to get abstracted
759 let data = wsJsonToRes<GetPersonDetailsResponse>(msg);
760 this.setState({ personRes: data, loading: false });
761 this.setPersonBlock();
762 restoreScrollPosition(this.context);
763 } else if (op == UserOperation.AddAdmin) {
764 let data = wsJsonToRes<AddAdminResponse>(msg);
765 this.setState(s => ((s.siteRes.admins = data.admins), s));
766 } else if (op == UserOperation.CreateCommentLike) {
767 let data = wsJsonToRes<CommentResponse>(msg);
768 createCommentLikeRes(data.comment_view, this.state.personRes?.comments);
769 this.setState(this.state);
771 op == UserOperation.EditComment ||
772 op == UserOperation.DeleteComment ||
773 op == UserOperation.RemoveComment
775 let data = wsJsonToRes<CommentResponse>(msg);
776 editCommentRes(data.comment_view, this.state.personRes?.comments);
777 this.setState(this.state);
778 } else if (op == UserOperation.CreateComment) {
779 let data = wsJsonToRes<CommentResponse>(msg);
780 let mui = UserService.Instance.myUserInfo;
781 if (data.comment_view.creator.id == mui?.local_user_view.person.id) {
782 toast(i18n.t("reply_sent"));
784 } else if (op == UserOperation.SaveComment) {
785 let data = wsJsonToRes<CommentResponse>(msg);
786 saveCommentRes(data.comment_view, this.state.personRes?.comments);
787 this.setState(this.state);
789 op == UserOperation.EditPost ||
790 op == UserOperation.DeletePost ||
791 op == UserOperation.RemovePost ||
792 op == UserOperation.LockPost ||
793 op == UserOperation.FeaturePost ||
794 op == UserOperation.SavePost
796 let data = wsJsonToRes<PostResponse>(msg);
797 editPostFindRes(data.post_view, this.state.personRes?.posts);
798 this.setState(this.state);
799 } else if (op == UserOperation.CreatePostLike) {
800 let data = wsJsonToRes<PostResponse>(msg);
801 createPostLikeFindRes(data.post_view, this.state.personRes?.posts);
802 this.setState(this.state);
803 } else if (op == UserOperation.BanPerson) {
804 let data = wsJsonToRes<BanPersonResponse>(msg);
805 let res = this.state.personRes;
807 .filter(c => c.creator.id == data.person_view.person.id)
808 .forEach(c => (c.creator.banned = data.banned));
810 .filter(c => c.creator.id == data.person_view.person.id)
811 .forEach(c => (c.creator.banned = data.banned));
812 let pv = res?.person_view;
814 if (pv?.person.id == data.person_view.person.id) {
815 pv.person.banned = data.banned;
817 this.setState(this.state);
818 } else if (op == UserOperation.BlockPerson) {
819 let data = wsJsonToRes<BlockPersonResponse>(msg);
820 updatePersonBlock(data);
821 this.setPersonBlock();
822 this.setState(this.state);
824 op == UserOperation.PurgePerson ||
825 op == UserOperation.PurgePost ||
826 op == UserOperation.PurgeComment ||
827 op == UserOperation.PurgeCommunity
829 let data = wsJsonToRes<PurgeItemResponse>(msg);
831 toast(i18n.t("purge_success"));
832 this.context.router.history.push(`/`);