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();
113 this.setPersonBlock();
117 let form: GetPersonDetails = {
118 username: this.state.userName,
119 sort: this.state.sort,
120 saved_only: this.state.view === PersonDetailsView.Saved,
121 page: this.state.page,
123 auth: authField(false),
125 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
128 get isCurrentUser() {
130 UserService.Instance.myUserInfo?.local_user_view.person.id ==
131 this.state.personRes.person_view.person.id
136 this.state.personBlocked = UserService.Instance.myUserInfo?.person_blocks
137 .map(a => a.target.id)
138 .includes(this.state.personRes?.person_view.person.id);
141 static getViewFromProps(view: string): PersonDetailsView {
142 return view ? PersonDetailsView[view] : PersonDetailsView.Overview;
145 static getSortTypeFromProps(sort: string): SortType {
146 return sort ? routeSortTypeToEnum(sort) : SortType.New;
149 static getPageFromProps(page: number): number {
150 return page ? Number(page) : 1;
153 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
154 let pathSplit = req.path.split("/");
155 let promises: Promise<any>[] = [];
157 // It can be /u/me, or /username/1
158 let idOrName = pathSplit[2];
159 let person_id: number;
160 let username: string;
161 if (isNaN(Number(idOrName))) {
164 person_id = Number(idOrName);
167 let view = this.getViewFromProps(pathSplit[4]);
168 let sort = this.getSortTypeFromProps(pathSplit[6]);
169 let page = this.getPageFromProps(Number(pathSplit[8]));
171 let form: GetPersonDetails = {
173 saved_only: view === PersonDetailsView.Saved,
177 setOptionalAuth(form, req.auth);
178 this.setIdOrName(form, person_id, username);
179 promises.push(req.client.getPersonDetails(form));
183 static setIdOrName(obj: any, id: number, name_: string) {
187 obj.username = name_;
191 componentWillUnmount() {
192 this.subscription.unsubscribe();
193 saveScrollPosition(this.context);
196 static getDerivedStateFromProps(props: any): ProfileProps {
198 view: this.getViewFromProps(props.match.params.view),
199 sort: this.getSortTypeFromProps(props.match.params.sort),
200 page: this.getPageFromProps(props.match.params.page),
201 person_id: Number(props.match.params.id) || null,
202 username: props.match.params.username,
206 componentDidUpdate(lastProps: any) {
207 // Necessary if you are on a post and you click another post (same route)
209 lastProps.location.pathname.split("/")[2] !==
210 lastProps.history.location.pathname.split("/")[2]
212 // Couldnt get a refresh working. This does for now.
217 get documentTitle(): string {
218 return `@${this.state.personRes.person_view.person.name} - ${this.state.siteRes.site_view.site.name}`;
221 get bioTag(): string {
222 return this.state.personRes.person_view.person.bio
223 ? previewLines(this.state.personRes.person_view.person.bio)
229 <div class="container">
230 {this.state.loading ? (
236 <div class="col-12 col-md-8">
239 title={this.documentTitle}
240 path={this.context.router.route.match.url}
241 description={this.bioTag}
242 image={this.state.personRes.person_view.person.avatar}
247 {!this.state.loading && this.selects()}
249 personRes={this.state.personRes}
250 admins={this.state.siteRes.admins}
251 sort={this.state.sort}
252 page={this.state.page}
255 this.state.siteRes.site_view.site.enable_downvotes
257 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
258 view={this.state.view}
259 onPageChange={this.handlePageChange}
263 {!this.state.loading && (
264 <div class="col-12 col-md-4">
266 {this.isCurrentUser && this.follows()}
277 <div class="btn-group btn-group-toggle flex-wrap mb-2">
279 className={`btn btn-outline-secondary pointer
280 ${this.state.view == PersonDetailsView.Overview && "active"}
285 value={PersonDetailsView.Overview}
286 checked={this.state.view === PersonDetailsView.Overview}
287 onChange={linkEvent(this, this.handleViewChange)}
292 className={`btn btn-outline-secondary pointer
293 ${this.state.view == PersonDetailsView.Comments && "active"}
298 value={PersonDetailsView.Comments}
299 checked={this.state.view == PersonDetailsView.Comments}
300 onChange={linkEvent(this, this.handleViewChange)}
305 className={`btn btn-outline-secondary pointer
306 ${this.state.view == PersonDetailsView.Posts && "active"}
311 value={PersonDetailsView.Posts}
312 checked={this.state.view == PersonDetailsView.Posts}
313 onChange={linkEvent(this, this.handleViewChange)}
318 className={`btn btn-outline-secondary pointer
319 ${this.state.view == PersonDetailsView.Saved && "active"}
324 value={PersonDetailsView.Saved}
325 checked={this.state.view == PersonDetailsView.Saved}
326 onChange={linkEvent(this, this.handleViewChange)}
336 <div className="mb-2">
337 <span class="mr-3">{this.viewRadios()}</span>
339 sort={this.state.sort}
340 onChange={this.handleSortChange}
345 href={`/feeds/u/${this.state.userName}.xml?sort=${this.state.sort}`}
349 <Icon icon="rss" classes="text-muted small mx-2" />
354 handleBlockPerson(personId: number) {
356 let blockUserForm: BlockPerson = {
361 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
364 handleUnblockPerson(recipientId: number) {
365 let blockUserForm: BlockPerson = {
366 person_id: recipientId,
370 WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
374 let pv = this.state.personRes?.person_view;
378 <BannerIconHeader banner={pv.person.banner} icon={pv.person.avatar} />
381 <div class="mb-0 d-flex flex-wrap">
383 {pv.person.display_name && (
384 <h5 class="mb-0">{pv.person.display_name}</h5>
386 <ul class="list-inline mb-2">
387 <li className="list-inline-item">
396 {pv.person.banned && (
397 <li className="list-inline-item badge badge-danger">
401 {pv.person.admin && (
402 <li className="list-inline-item badge badge-light">
406 {pv.person.bot_account && (
407 <li className="list-inline-item badge badge-light">
408 {i18n.t("bot_account").toLowerCase()}
413 <div className="flex-grow-1 unselectable pointer mx-2"></div>
414 {!this.isCurrentUser && UserService.Instance.myUserInfo && (
417 className={`d-flex align-self-start btn btn-secondary mr-2 ${
418 !pv.person.matrix_user_id && "invisible"
421 href={`https://matrix.to/#/${pv.person.matrix_user_id}`}
423 {i18n.t("send_secure_message")}
426 className={"d-flex align-self-start btn btn-secondary mr-2"}
427 to={`/create_private_message/recipient/${pv.person.id}`}
429 {i18n.t("send_message")}
431 {this.state.personBlocked ? (
433 className={"d-flex align-self-start btn btn-secondary"}
436 this.handleUnblockPerson
439 {i18n.t("unblock_user")}
443 className={"d-flex align-self-start btn btn-secondary"}
444 onClick={linkEvent(pv.person.id, this.handleBlockPerson)}
446 {i18n.t("block_user")}
453 <div className="d-flex align-items-center mb-2">
456 dangerouslySetInnerHTML={mdToHtml(pv.person.bio)}
461 <ul class="list-inline mb-2">
462 <li className="list-inline-item badge badge-light">
463 {i18n.t("number_of_posts", {
464 count: pv.counts.post_count,
465 formattedCount: numToSI(pv.counts.post_count),
468 <li className="list-inline-item badge badge-light">
469 {i18n.t("number_of_comments", {
470 count: pv.counts.comment_count,
471 formattedCount: numToSI(pv.counts.comment_count),
476 <div class="text-muted">
477 {i18n.t("joined")}{" "}
478 <MomentTime data={pv.person} showAgo ignoreUpdated />
480 <div className="d-flex align-items-center text-muted mb-2">
482 <span className="ml-2">
483 {i18n.t("cake_day_title")}{" "}
484 {moment.utc(pv.person.published).local().format("MMM DD, YYYY")}
496 {this.state.personRes.moderates.length > 0 && (
497 <div class="card border-secondary mb-3">
498 <div class="card-body">
499 <h5>{i18n.t("moderates")}</h5>
500 <ul class="list-unstyled mb-0">
501 {this.state.personRes.moderates.map(cmv => (
503 <CommunityLink community={cmv.community} />
515 let follows = UserService.Instance.myUserInfo.follows;
518 {follows.length > 0 && (
519 <div class="card border-secondary mb-3">
520 <div class="card-body">
521 <h5>{i18n.t("subscribed")}</h5>
522 <ul class="list-unstyled mb-0">
523 {follows.map(cfv => (
525 <CommunityLink community={cfv.community} />
536 updateUrl(paramUpdates: UrlParams) {
537 const page = paramUpdates.page || this.state.page;
538 const viewStr = paramUpdates.view || PersonDetailsView[this.state.view];
539 const sortStr = paramUpdates.sort || this.state.sort;
541 let typeView = `/u/${this.state.userName}`;
543 this.props.history.push(
544 `${typeView}/view/${viewStr}/sort/${sortStr}/page/${page}`
546 this.state.loading = true;
547 this.setState(this.state);
548 this.fetchUserData();
551 handlePageChange(page: number) {
552 this.updateUrl({ page });
555 handleSortChange(val: SortType) {
556 this.updateUrl({ sort: val, page: 1 });
559 handleViewChange(i: Profile, event: any) {
561 view: PersonDetailsView[Number(event.target.value)],
566 parseMessage(msg: any) {
567 let op = wsUserOp(msg);
570 toast(i18n.t(msg.error), "danger");
571 if (msg.error == "couldnt_find_that_username_or_email") {
572 this.context.router.history.push("/");
575 } else if (msg.reconnect) {
576 this.fetchUserData();
577 } else if (op == UserOperation.GetPersonDetails) {
578 // Since the PersonDetails contains posts/comments as well as some general user info we listen here as well
579 // and set the parent state if it is not set or differs
580 // TODO this might need to get abstracted
581 let data = wsJsonToRes<GetPersonDetailsResponse>(msg).data;
582 this.state.personRes = data;
584 this.state.loading = false;
585 this.setPersonBlock();
586 this.setState(this.state);
587 restoreScrollPosition(this.context);
588 } else if (op == UserOperation.AddAdmin) {
589 let data = wsJsonToRes<AddAdminResponse>(msg).data;
590 this.state.siteRes.admins = data.admins;
591 this.setState(this.state);
592 } else if (op == UserOperation.CreateCommentLike) {
593 let data = wsJsonToRes<CommentResponse>(msg).data;
594 createCommentLikeRes(data.comment_view, this.state.personRes.comments);
595 this.setState(this.state);
597 op == UserOperation.EditComment ||
598 op == UserOperation.DeleteComment ||
599 op == UserOperation.RemoveComment
601 let data = wsJsonToRes<CommentResponse>(msg).data;
602 editCommentRes(data.comment_view, this.state.personRes.comments);
603 this.setState(this.state);
604 } else if (op == UserOperation.CreateComment) {
605 let data = wsJsonToRes<CommentResponse>(msg).data;
607 UserService.Instance.myUserInfo &&
608 data.comment_view.creator.id ==
609 UserService.Instance.myUserInfo.local_user_view.person.id
611 toast(i18n.t("reply_sent"));
613 } else if (op == UserOperation.SaveComment) {
614 let data = wsJsonToRes<CommentResponse>(msg).data;
615 saveCommentRes(data.comment_view, this.state.personRes.comments);
616 this.setState(this.state);
618 op == UserOperation.EditPost ||
619 op == UserOperation.DeletePost ||
620 op == UserOperation.RemovePost ||
621 op == UserOperation.LockPost ||
622 op == UserOperation.StickyPost ||
623 op == UserOperation.SavePost
625 let data = wsJsonToRes<PostResponse>(msg).data;
626 editPostFindRes(data.post_view, this.state.personRes.posts);
627 this.setState(this.state);
628 } else if (op == UserOperation.CreatePostLike) {
629 let data = wsJsonToRes<PostResponse>(msg).data;
630 createPostLikeFindRes(data.post_view, this.state.personRes.posts);
631 this.setState(this.state);
632 } else if (op == UserOperation.BanPerson) {
633 let data = wsJsonToRes<BanPersonResponse>(msg).data;
634 this.state.personRes.comments
635 .filter(c => c.creator.id == data.person_view.person.id)
636 .forEach(c => (c.creator.banned = data.banned));
637 this.state.personRes.posts
638 .filter(c => c.creator.id == data.person_view.person.id)
639 .forEach(c => (c.creator.banned = data.banned));
640 this.setState(this.state);
641 } else if (op == UserOperation.BlockPerson) {
642 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
643 updatePersonBlock(data);
644 this.setPersonBlock();
645 this.setState(this.state);