1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
9 GetPersonDetailsResponse,
14 } from "lemmy-js-client";
15 import moment from "moment";
16 import { Subscription } from "rxjs";
17 import { i18n } from "../../i18next";
18 import { InitialFetchRequest, PersonDetailsView } from "../../interfaces";
19 import { UserService, WebSocketService } from "../../services";
23 createPostLikeFindRes,
31 restoreScrollPosition,
45 import { BannerIconHeader } from "../common/banner-icon-header";
46 import { HtmlTags } from "../common/html-tags";
47 import { Icon, Spinner } from "../common/icon";
48 import { MomentTime } from "../common/moment-time";
49 import { SortSelect } from "../common/sort-select";
50 import { CommunityLink } from "../community/community-link";
51 import { PersonDetails } from "./person-details";
52 import { PersonListing } from "./person-listing";
54 interface ProfileState {
55 personRes: GetPersonDetailsResponse;
57 view: PersonDetailsView;
61 siteRes: GetSiteResponse;
64 interface ProfileProps {
65 view: PersonDetailsView;
68 person_id: number | null;
78 export class Profile extends Component<any, ProfileState> {
79 private isoData = setIsoData(this.context);
80 private subscription: Subscription;
81 private emptyState: ProfileState = {
83 userName: getUsernameFromProps(this.props),
85 view: Profile.getViewFromProps(this.props.match.view),
86 sort: Profile.getSortTypeFromProps(this.props.match.sort),
87 page: Profile.getPageFromProps(this.props.match.page),
88 siteRes: this.isoData.site_res,
91 constructor(props: any, context: any) {
92 super(props, context);
94 this.state = this.emptyState;
95 this.handleSortChange = this.handleSortChange.bind(this);
96 this.handlePageChange = this.handlePageChange.bind(this);
98 this.parseMessage = this.parseMessage.bind(this);
99 this.subscription = wsSubscribe(this.parseMessage);
101 // Only fetch the data if coming from another route
102 if (this.isoData.path == this.context.router.route.match.url) {
103 this.state.personRes = this.isoData.routeData[0];
104 this.state.loading = false;
106 this.fetchUserData();
113 let form: GetPersonDetails = {
114 username: this.state.userName,
115 sort: this.state.sort,
116 saved_only: this.state.view === PersonDetailsView.Saved,
117 page: this.state.page,
119 auth: authField(false),
121 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
124 get isCurrentUser() {
126 UserService.Instance.myUserInfo?.local_user_view.person.id ==
127 this.state.personRes.person_view.person.id
131 static getViewFromProps(view: string): PersonDetailsView {
132 return view ? PersonDetailsView[view] : PersonDetailsView.Overview;
135 static getSortTypeFromProps(sort: string): SortType {
136 return sort ? routeSortTypeToEnum(sort) : SortType.New;
139 static getPageFromProps(page: number): number {
140 return page ? Number(page) : 1;
143 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
144 let pathSplit = req.path.split("/");
145 let promises: Promise<any>[] = [];
147 // It can be /u/me, or /username/1
148 let idOrName = pathSplit[2];
149 let person_id: number;
150 let username: string;
151 if (isNaN(Number(idOrName))) {
154 person_id = Number(idOrName);
157 let view = this.getViewFromProps(pathSplit[4]);
158 let sort = this.getSortTypeFromProps(pathSplit[6]);
159 let page = this.getPageFromProps(Number(pathSplit[8]));
161 let form: GetPersonDetails = {
163 saved_only: view === PersonDetailsView.Saved,
167 setOptionalAuth(form, req.auth);
168 this.setIdOrName(form, person_id, username);
169 promises.push(req.client.getPersonDetails(form));
173 static setIdOrName(obj: any, id: number, name_: string) {
177 obj.username = name_;
181 componentWillUnmount() {
182 this.subscription.unsubscribe();
183 saveScrollPosition(this.context);
186 static getDerivedStateFromProps(props: any): ProfileProps {
188 view: this.getViewFromProps(props.match.params.view),
189 sort: this.getSortTypeFromProps(props.match.params.sort),
190 page: this.getPageFromProps(props.match.params.page),
191 person_id: Number(props.match.params.id) || null,
192 username: props.match.params.username,
196 componentDidUpdate(lastProps: any) {
197 // Necessary if you are on a post and you click another post (same route)
199 lastProps.location.pathname.split("/")[2] !==
200 lastProps.history.location.pathname.split("/")[2]
202 // Couldnt get a refresh working. This does for now.
207 get documentTitle(): string {
208 return `@${this.state.personRes.person_view.person.name} - ${this.state.siteRes.site_view.site.name}`;
211 get bioTag(): string {
212 return this.state.personRes.person_view.person.bio
213 ? previewLines(this.state.personRes.person_view.person.bio)
219 <div class="container">
220 {this.state.loading ? (
226 <div class="col-12 col-md-8">
229 title={this.documentTitle}
230 path={this.context.router.route.match.url}
231 description={this.bioTag}
232 image={this.state.personRes.person_view.person.avatar}
237 {!this.state.loading && this.selects()}
239 personRes={this.state.personRes}
240 admins={this.state.siteRes.admins}
241 sort={this.state.sort}
242 page={this.state.page}
245 this.state.siteRes.site_view.site.enable_downvotes
247 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
248 view={this.state.view}
249 onPageChange={this.handlePageChange}
253 {!this.state.loading && (
254 <div class="col-12 col-md-4">
256 {this.isCurrentUser && this.follows()}
267 <div class="btn-group btn-group-toggle flex-wrap mb-2">
269 className={`btn btn-outline-secondary pointer
270 ${this.state.view == PersonDetailsView.Overview && "active"}
275 value={PersonDetailsView.Overview}
276 checked={this.state.view === PersonDetailsView.Overview}
277 onChange={linkEvent(this, this.handleViewChange)}
282 className={`btn btn-outline-secondary pointer
283 ${this.state.view == PersonDetailsView.Comments && "active"}
288 value={PersonDetailsView.Comments}
289 checked={this.state.view == PersonDetailsView.Comments}
290 onChange={linkEvent(this, this.handleViewChange)}
295 className={`btn btn-outline-secondary pointer
296 ${this.state.view == PersonDetailsView.Posts && "active"}
301 value={PersonDetailsView.Posts}
302 checked={this.state.view == PersonDetailsView.Posts}
303 onChange={linkEvent(this, this.handleViewChange)}
308 className={`btn btn-outline-secondary pointer
309 ${this.state.view == PersonDetailsView.Saved && "active"}
314 value={PersonDetailsView.Saved}
315 checked={this.state.view == PersonDetailsView.Saved}
316 onChange={linkEvent(this, this.handleViewChange)}
326 <div className="mb-2">
327 <span class="mr-3">{this.viewRadios()}</span>
329 sort={this.state.sort}
330 onChange={this.handleSortChange}
335 href={`/feeds/u/${this.state.userName}.xml?sort=${this.state.sort}`}
339 <Icon icon="rss" classes="text-muted small mx-2" />
346 let pv = this.state.personRes?.person_view;
350 <BannerIconHeader banner={pv.person.banner} icon={pv.person.avatar} />
353 <div class="mb-0 d-flex flex-wrap">
355 {pv.person.display_name && (
356 <h5 class="mb-0">{pv.person.display_name}</h5>
358 <ul class="list-inline mb-2">
359 <li className="list-inline-item">
368 {pv.person.banned && (
369 <li className="list-inline-item badge badge-danger">
375 <div className="flex-grow-1 unselectable pointer mx-2"></div>
376 {!this.isCurrentUser && (
379 className={`d-flex align-self-start btn btn-secondary mr-2 ${
380 !pv.person.matrix_user_id && "invisible"
383 href={`https://matrix.to/#/${pv.person.matrix_user_id}`}
385 {i18n.t("send_secure_message")}
388 className={"d-flex align-self-start btn btn-secondary"}
389 to={`/create_private_message/recipient/${pv.person.id}`}
391 {i18n.t("send_message")}
397 <div className="d-flex align-items-center mb-2">
400 dangerouslySetInnerHTML={mdToHtml(pv.person.bio)}
405 <ul class="list-inline mb-2">
406 <li className="list-inline-item badge badge-light">
407 {i18n.t("number_of_posts", {
408 count: pv.counts.post_count,
409 formattedCount: numToSI(pv.counts.post_count),
412 <li className="list-inline-item badge badge-light">
413 {i18n.t("number_of_comments", {
414 count: pv.counts.comment_count,
415 formattedCount: numToSI(pv.counts.comment_count),
420 <div class="text-muted">
421 {i18n.t("joined")}{" "}
422 <MomentTime data={pv.person} showAgo ignoreUpdated />
424 <div className="d-flex align-items-center text-muted mb-2">
426 <span className="ml-2">
427 {i18n.t("cake_day_title")}{" "}
428 {moment.utc(pv.person.published).local().format("MMM DD, YYYY")}
440 {this.state.personRes.moderates.length > 0 && (
441 <div class="card border-secondary mb-3">
442 <div class="card-body">
443 <h5>{i18n.t("moderates")}</h5>
444 <ul class="list-unstyled mb-0">
445 {this.state.personRes.moderates.map(cmv => (
447 <CommunityLink community={cmv.community} />
459 let follows = UserService.Instance.myUserInfo.follows;
462 {follows.length > 0 && (
463 <div class="card border-secondary mb-3">
464 <div class="card-body">
465 <h5>{i18n.t("subscribed")}</h5>
466 <ul class="list-unstyled mb-0">
467 {follows.map(cfv => (
469 <CommunityLink community={cfv.community} />
480 updateUrl(paramUpdates: UrlParams) {
481 const page = paramUpdates.page || this.state.page;
482 const viewStr = paramUpdates.view || PersonDetailsView[this.state.view];
483 const sortStr = paramUpdates.sort || this.state.sort;
485 let typeView = `/u/${this.state.userName}`;
487 this.props.history.push(
488 `${typeView}/view/${viewStr}/sort/${sortStr}/page/${page}`
490 this.state.loading = true;
491 this.setState(this.state);
492 this.fetchUserData();
495 handlePageChange(page: number) {
496 this.updateUrl({ page });
499 handleSortChange(val: SortType) {
500 this.updateUrl({ sort: val, page: 1 });
503 handleViewChange(i: Profile, event: any) {
505 view: PersonDetailsView[Number(event.target.value)],
510 parseMessage(msg: any) {
511 let op = wsUserOp(msg);
514 toast(i18n.t(msg.error), "danger");
515 if (msg.error == "couldnt_find_that_username_or_email") {
516 this.context.router.history.push("/");
519 } else if (msg.reconnect) {
520 this.fetchUserData();
521 } else if (op == UserOperation.GetPersonDetails) {
522 // Since the PersonDetails contains posts/comments as well as some general user info we listen here as well
523 // and set the parent state if it is not set or differs
524 // TODO this might need to get abstracted
525 let data = wsJsonToRes<GetPersonDetailsResponse>(msg).data;
526 this.state.personRes = data;
528 this.state.loading = false;
529 this.setState(this.state);
530 restoreScrollPosition(this.context);
531 } else if (op == UserOperation.AddAdmin) {
532 let data = wsJsonToRes<AddAdminResponse>(msg).data;
533 this.state.siteRes.admins = data.admins;
534 this.setState(this.state);
535 } else if (op == UserOperation.CreateCommentLike) {
536 let data = wsJsonToRes<CommentResponse>(msg).data;
537 createCommentLikeRes(data.comment_view, this.state.personRes.comments);
538 this.setState(this.state);
540 op == UserOperation.EditComment ||
541 op == UserOperation.DeleteComment ||
542 op == UserOperation.RemoveComment
544 let data = wsJsonToRes<CommentResponse>(msg).data;
545 editCommentRes(data.comment_view, this.state.personRes.comments);
546 this.setState(this.state);
547 } else if (op == UserOperation.CreateComment) {
548 let data = wsJsonToRes<CommentResponse>(msg).data;
550 UserService.Instance.myUserInfo &&
551 data.comment_view.creator.id ==
552 UserService.Instance.myUserInfo.local_user_view.person.id
554 toast(i18n.t("reply_sent"));
556 } else if (op == UserOperation.SaveComment) {
557 let data = wsJsonToRes<CommentResponse>(msg).data;
558 saveCommentRes(data.comment_view, this.state.personRes.comments);
559 this.setState(this.state);
561 op == UserOperation.EditPost ||
562 op == UserOperation.DeletePost ||
563 op == UserOperation.RemovePost ||
564 op == UserOperation.LockPost ||
565 op == UserOperation.StickyPost ||
566 op == UserOperation.SavePost
568 let data = wsJsonToRes<PostResponse>(msg).data;
569 editPostFindRes(data.post_view, this.state.personRes.posts);
570 this.setState(this.state);
571 } else if (op == UserOperation.CreatePostLike) {
572 let data = wsJsonToRes<PostResponse>(msg).data;
573 createPostLikeFindRes(data.post_view, this.state.personRes.posts);
574 this.setState(this.state);
575 } else if (op == UserOperation.BanPerson) {
576 let data = wsJsonToRes<BanPersonResponse>(msg).data;
577 this.state.personRes.comments
578 .filter(c => c.creator.id == data.person_view.person.id)
579 .forEach(c => (c.creator.banned = data.banned));
580 this.state.personRes.posts
581 .filter(c => c.creator.id == data.person_view.person.id)
582 .forEach(c => (c.creator.banned = data.banned));
583 this.setState(this.state);
584 } else if (op == UserOperation.BlockPerson) {
585 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
586 updatePersonBlock(data);