1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
10 GetPersonDetailsResponse,
15 } from "lemmy-js-client";
16 import moment from "moment";
17 import { Subscription } from "rxjs";
18 import { i18n } from "../../i18next";
19 import { InitialFetchRequest, PersonDetailsView } from "../../interfaces";
20 import { UserService, WebSocketService } from "../../services";
24 createPostLikeFindRes,
32 restoreScrollPosition,
46 import { BannerIconHeader } from "../common/banner-icon-header";
47 import { HtmlTags } from "../common/html-tags";
48 import { Icon, Spinner } from "../common/icon";
49 import { MomentTime } from "../common/moment-time";
50 import { SortSelect } from "../common/sort-select";
51 import { CommunityLink } from "../community/community-link";
52 import { PersonDetails } from "./person-details";
53 import { PersonListing } from "./person-listing";
55 interface ProfileState {
56 personRes: GetPersonDetailsResponse;
58 view: PersonDetailsView;
62 personBlocked: boolean;
63 siteRes: GetSiteResponse;
66 interface ProfileProps {
67 view: PersonDetailsView;
70 person_id: number | null;
80 export class Profile extends Component<any, ProfileState> {
81 private isoData = setIsoData(this.context);
82 private subscription: Subscription;
83 private emptyState: ProfileState = {
85 userName: getUsernameFromProps(this.props),
87 view: Profile.getViewFromProps(this.props.match.view),
88 sort: Profile.getSortTypeFromProps(this.props.match.sort),
89 page: Profile.getPageFromProps(this.props.match.page),
91 siteRes: this.isoData.site_res,
94 constructor(props: any, context: any) {
95 super(props, context);
97 this.state = this.emptyState;
98 this.handleSortChange = this.handleSortChange.bind(this);
99 this.handlePageChange = this.handlePageChange.bind(this);
101 this.parseMessage = this.parseMessage.bind(this);
102 this.subscription = wsSubscribe(this.parseMessage);
104 // Only fetch the data if coming from another route
105 if (this.isoData.path == this.context.router.route.match.url) {
106 this.state.personRes = this.isoData.routeData[0];
107 this.state.loading = false;
109 this.fetchUserData();
112 this.setPersonBlock();
116 let form: GetPersonDetails = {
117 username: this.state.userName,
118 sort: this.state.sort,
119 saved_only: this.state.view === PersonDetailsView.Saved,
120 page: this.state.page,
122 auth: authField(false),
124 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
127 get isCurrentUser() {
129 UserService.Instance.myUserInfo?.local_user_view.person.id ==
130 this.state.personRes.person_view.person.id
135 this.state.personBlocked = UserService.Instance.myUserInfo?.person_blocks
136 .map(a => a.target.id)
137 .includes(this.state.personRes?.person_view.person.id);
140 static getViewFromProps(view: string): PersonDetailsView {
141 return view ? PersonDetailsView[view] : PersonDetailsView.Overview;
144 static getSortTypeFromProps(sort: string): SortType {
145 return sort ? routeSortTypeToEnum(sort) : SortType.New;
148 static getPageFromProps(page: number): number {
149 return page ? Number(page) : 1;
152 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
153 let pathSplit = req.path.split("/");
154 let promises: Promise<any>[] = [];
156 // It can be /u/me, or /username/1
157 let idOrName = pathSplit[2];
158 let person_id: number;
159 let username: string;
160 if (isNaN(Number(idOrName))) {
163 person_id = Number(idOrName);
166 let view = this.getViewFromProps(pathSplit[4]);
167 let sort = this.getSortTypeFromProps(pathSplit[6]);
168 let page = this.getPageFromProps(Number(pathSplit[8]));
170 let form: GetPersonDetails = {
172 saved_only: view === PersonDetailsView.Saved,
176 setOptionalAuth(form, req.auth);
177 this.setIdOrName(form, person_id, username);
178 promises.push(req.client.getPersonDetails(form));
182 static setIdOrName(obj: any, id: number, name_: string) {
186 obj.username = name_;
190 componentDidMount() {
194 componentWillUnmount() {
195 this.subscription.unsubscribe();
196 saveScrollPosition(this.context);
199 static getDerivedStateFromProps(props: any): ProfileProps {
201 view: this.getViewFromProps(props.match.params.view),
202 sort: this.getSortTypeFromProps(props.match.params.sort),
203 page: this.getPageFromProps(props.match.params.page),
204 person_id: Number(props.match.params.id) || null,
205 username: props.match.params.username,
209 componentDidUpdate(lastProps: any) {
210 // Necessary if you are on a post and you click another post (same route)
212 lastProps.location.pathname.split("/")[2] !==
213 lastProps.history.location.pathname.split("/")[2]
215 // Couldnt get a refresh working. This does for now.
220 get documentTitle(): string {
221 return `@${this.state.personRes.person_view.person.name} - ${this.state.siteRes.site_view.site.name}`;
224 get bioTag(): string {
225 return this.state.personRes.person_view.person.bio
226 ? previewLines(this.state.personRes.person_view.person.bio)
232 <div class="container">
233 {this.state.loading ? (
239 <div class="col-12 col-md-8">
242 title={this.documentTitle}
243 path={this.context.router.route.match.url}
244 description={this.bioTag}
245 image={this.state.personRes.person_view.person.avatar}
250 {!this.state.loading && this.selects()}
252 personRes={this.state.personRes}
253 admins={this.state.siteRes.admins}
254 sort={this.state.sort}
255 page={this.state.page}
258 this.state.siteRes.site_view.site.enable_downvotes
260 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
261 view={this.state.view}
262 onPageChange={this.handlePageChange}
266 {!this.state.loading && (
267 <div class="col-12 col-md-4">
269 {this.isCurrentUser && this.follows()}
280 <div class="btn-group btn-group-toggle flex-wrap mb-2">
282 className={`btn btn-outline-secondary pointer
283 ${this.state.view == PersonDetailsView.Overview && "active"}
288 value={PersonDetailsView.Overview}
289 checked={this.state.view === PersonDetailsView.Overview}
290 onChange={linkEvent(this, this.handleViewChange)}
295 className={`btn btn-outline-secondary pointer
296 ${this.state.view == PersonDetailsView.Comments && "active"}
301 value={PersonDetailsView.Comments}
302 checked={this.state.view == PersonDetailsView.Comments}
303 onChange={linkEvent(this, this.handleViewChange)}
308 className={`btn btn-outline-secondary pointer
309 ${this.state.view == PersonDetailsView.Posts && "active"}
314 value={PersonDetailsView.Posts}
315 checked={this.state.view == PersonDetailsView.Posts}
316 onChange={linkEvent(this, this.handleViewChange)}
321 className={`btn btn-outline-secondary pointer
322 ${this.state.view == PersonDetailsView.Saved && "active"}
327 value={PersonDetailsView.Saved}
328 checked={this.state.view == PersonDetailsView.Saved}
329 onChange={linkEvent(this, this.handleViewChange)}
339 <div className="mb-2">
340 <span class="mr-3">{this.viewRadios()}</span>
342 sort={this.state.sort}
343 onChange={this.handleSortChange}
348 href={`/feeds/u/${this.state.userName}.xml?sort=${this.state.sort}`}
352 <Icon icon="rss" classes="text-muted small mx-2" />
357 handleBlockPerson(personId: number) {
359 let blockUserForm: BlockPerson = {
364 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
367 handleUnblockPerson(recipientId: number) {
368 let blockUserForm: BlockPerson = {
369 person_id: recipientId,
373 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
377 let pv = this.state.personRes?.person_view;
381 <BannerIconHeader banner={pv.person.banner} icon={pv.person.avatar} />
384 <div class="mb-0 d-flex flex-wrap">
386 {pv.person.display_name && (
387 <h5 class="mb-0">{pv.person.display_name}</h5>
389 <ul class="list-inline mb-2">
390 <li className="list-inline-item">
399 {pv.person.banned && (
400 <li className="list-inline-item badge badge-danger">
404 {pv.person.admin && (
405 <li className="list-inline-item badge badge-light">
409 {pv.person.bot_account && (
410 <li className="list-inline-item badge badge-light">
411 {i18n.t("bot_account").toLowerCase()}
416 <div className="flex-grow-1 unselectable pointer mx-2"></div>
417 {!this.isCurrentUser && UserService.Instance.myUserInfo && (
420 className={`d-flex align-self-start btn btn-secondary mr-2 ${
421 !pv.person.matrix_user_id && "invisible"
424 href={`https://matrix.to/#/${pv.person.matrix_user_id}`}
426 {i18n.t("send_secure_message")}
429 className={"d-flex align-self-start btn btn-secondary mr-2"}
430 to={`/create_private_message/recipient/${pv.person.id}`}
432 {i18n.t("send_message")}
434 {this.state.personBlocked ? (
436 className={"d-flex align-self-start btn btn-secondary"}
439 this.handleUnblockPerson
442 {i18n.t("unblock_user")}
446 className={"d-flex align-self-start btn btn-secondary"}
447 onClick={linkEvent(pv.person.id, this.handleBlockPerson)}
449 {i18n.t("block_user")}
456 <div className="d-flex align-items-center mb-2">
459 dangerouslySetInnerHTML={mdToHtml(pv.person.bio)}
464 <ul class="list-inline mb-2">
465 <li className="list-inline-item badge badge-light">
466 {i18n.t("number_of_posts", {
467 count: pv.counts.post_count,
468 formattedCount: numToSI(pv.counts.post_count),
471 <li className="list-inline-item badge badge-light">
472 {i18n.t("number_of_comments", {
473 count: pv.counts.comment_count,
474 formattedCount: numToSI(pv.counts.comment_count),
479 <div class="text-muted">
480 {i18n.t("joined")}{" "}
481 <MomentTime data={pv.person} showAgo ignoreUpdated />
483 <div className="d-flex align-items-center text-muted mb-2">
485 <span className="ml-2">
486 {i18n.t("cake_day_title")}{" "}
487 {moment.utc(pv.person.published).local().format("MMM DD, YYYY")}
499 {this.state.personRes.moderates.length > 0 && (
500 <div class="card border-secondary mb-3">
501 <div class="card-body">
502 <h5>{i18n.t("moderates")}</h5>
503 <ul class="list-unstyled mb-0">
504 {this.state.personRes.moderates.map(cmv => (
506 <CommunityLink community={cmv.community} />
518 let follows = UserService.Instance.myUserInfo.follows;
521 {follows.length > 0 && (
522 <div class="card border-secondary mb-3">
523 <div class="card-body">
524 <h5>{i18n.t("subscribed")}</h5>
525 <ul class="list-unstyled mb-0">
526 {follows.map(cfv => (
528 <CommunityLink community={cfv.community} />
539 updateUrl(paramUpdates: UrlParams) {
540 const page = paramUpdates.page || this.state.page;
541 const viewStr = paramUpdates.view || PersonDetailsView[this.state.view];
542 const sortStr = paramUpdates.sort || this.state.sort;
544 let typeView = `/u/${this.state.userName}`;
546 this.props.history.push(
547 `${typeView}/view/${viewStr}/sort/${sortStr}/page/${page}`
549 this.state.loading = true;
550 this.setState(this.state);
551 this.fetchUserData();
554 handlePageChange(page: number) {
555 this.updateUrl({ page });
558 handleSortChange(val: SortType) {
559 this.updateUrl({ sort: val, page: 1 });
562 handleViewChange(i: Profile, event: any) {
564 view: PersonDetailsView[Number(event.target.value)],
569 parseMessage(msg: any) {
570 let op = wsUserOp(msg);
573 toast(i18n.t(msg.error), "danger");
574 if (msg.error == "couldnt_find_that_username_or_email") {
575 this.context.router.history.push("/");
578 } else if (msg.reconnect) {
579 this.fetchUserData();
580 } else if (op == UserOperation.GetPersonDetails) {
581 // Since the PersonDetails contains posts/comments as well as some general user info we listen here as well
582 // and set the parent state if it is not set or differs
583 // TODO this might need to get abstracted
584 let data = wsJsonToRes<GetPersonDetailsResponse>(msg).data;
585 this.state.personRes = data;
587 this.state.loading = false;
588 this.setPersonBlock();
589 this.setState(this.state);
590 restoreScrollPosition(this.context);
591 } else if (op == UserOperation.AddAdmin) {
592 let data = wsJsonToRes<AddAdminResponse>(msg).data;
593 this.state.siteRes.admins = data.admins;
594 this.setState(this.state);
595 } else if (op == UserOperation.CreateCommentLike) {
596 let data = wsJsonToRes<CommentResponse>(msg).data;
597 createCommentLikeRes(data.comment_view, this.state.personRes.comments);
598 this.setState(this.state);
600 op == UserOperation.EditComment ||
601 op == UserOperation.DeleteComment ||
602 op == UserOperation.RemoveComment
604 let data = wsJsonToRes<CommentResponse>(msg).data;
605 editCommentRes(data.comment_view, this.state.personRes.comments);
606 this.setState(this.state);
607 } else if (op == UserOperation.CreateComment) {
608 let data = wsJsonToRes<CommentResponse>(msg).data;
610 UserService.Instance.myUserInfo &&
611 data.comment_view.creator.id ==
612 UserService.Instance.myUserInfo?.local_user_view.person.id
614 toast(i18n.t("reply_sent"));
616 } else if (op == UserOperation.SaveComment) {
617 let data = wsJsonToRes<CommentResponse>(msg).data;
618 saveCommentRes(data.comment_view, this.state.personRes.comments);
619 this.setState(this.state);
621 op == UserOperation.EditPost ||
622 op == UserOperation.DeletePost ||
623 op == UserOperation.RemovePost ||
624 op == UserOperation.LockPost ||
625 op == UserOperation.StickyPost ||
626 op == UserOperation.SavePost
628 let data = wsJsonToRes<PostResponse>(msg).data;
629 editPostFindRes(data.post_view, this.state.personRes.posts);
630 this.setState(this.state);
631 } else if (op == UserOperation.CreatePostLike) {
632 let data = wsJsonToRes<PostResponse>(msg).data;
633 createPostLikeFindRes(data.post_view, this.state.personRes.posts);
634 this.setState(this.state);
635 } else if (op == UserOperation.BanPerson) {
636 let data = wsJsonToRes<BanPersonResponse>(msg).data;
637 this.state.personRes.comments
638 .filter(c => c.creator.id == data.person_view.person.id)
639 .forEach(c => (c.creator.banned = data.banned));
640 this.state.personRes.posts
641 .filter(c => c.creator.id == data.person_view.person.id)
642 .forEach(c => (c.creator.banned = data.banned));
643 this.setState(this.state);
644 } else if (op == UserOperation.BlockPerson) {
645 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
646 updatePersonBlock(data);
647 this.setPersonBlock();
648 this.setState(this.state);