1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Link } from 'inferno-router';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
15 WebSocketJsonResponse,
19 } from 'lemmy-js-client';
20 import { UserDetailsView } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
26 capitalizeFirstLetter,
37 import { UserListing } from './user-listing';
38 import { SortSelect } from './sort-select';
39 import { ListingTypeSelect } from './listing-type-select';
40 import { MomentTime } from './moment-time';
41 import { i18n } from '../i18next';
42 import moment from 'moment';
43 import { UserDetails } from './user-details';
44 import { MarkdownTextArea } from './markdown-textarea';
45 import { ImageUploadForm } from './image-upload-form';
46 import { BannerIconHeader } from './banner-icon-header';
52 follows: CommunityUser[];
53 moderates: CommunityUser[];
54 view: UserDetailsView;
58 userSettingsForm: UserSettingsForm;
59 userSettingsLoading: boolean;
60 deleteAccountLoading: boolean;
61 deleteAccountShowConfirm: boolean;
62 deleteAccountForm: DeleteAccountForm;
63 siteRes: GetSiteResponse;
67 view: UserDetailsView;
70 user_id: number | null;
80 export class User extends Component<any, UserState> {
81 private subscription: Subscription;
82 private emptyState: UserState = {
87 number_of_posts: null,
89 number_of_comments: null,
101 view: User.getViewFromProps(this.props.match.view),
102 sort: User.getSortTypeFromProps(this.props.match.sort),
103 page: User.getPageFromProps(this.props.match.page),
107 default_sort_type: null,
108 default_listing_type: null,
111 send_notifications_to_email: null,
114 preferred_username: null,
116 userSettingsLoading: null,
117 deleteAccountLoading: null,
118 deleteAccountShowConfirm: false,
129 creator_id: undefined,
130 published: undefined,
131 creator_name: undefined,
132 number_of_users: undefined,
133 number_of_posts: undefined,
134 number_of_comments: undefined,
135 number_of_communities: undefined,
136 enable_downvotes: undefined,
137 open_registration: undefined,
138 enable_nsfw: undefined,
141 creator_preferred_username: undefined,
145 federated_instances: undefined,
149 constructor(props: any, context: any) {
150 super(props, context);
152 this.state = this.emptyState;
153 this.handleSortChange = this.handleSortChange.bind(this);
154 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
157 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
160 this.handlePageChange = this.handlePageChange.bind(this);
161 this.handleUserSettingsBioChange = this.handleUserSettingsBioChange.bind(
165 this.handleAvatarUpload = this.handleAvatarUpload.bind(this);
166 this.handleAvatarRemove = this.handleAvatarRemove.bind(this);
168 this.handleBannerUpload = this.handleBannerUpload.bind(this);
169 this.handleBannerRemove = this.handleBannerRemove.bind(this);
171 this.state.user_id = Number(this.props.match.params.id) || null;
172 this.state.username = this.props.match.params.username;
174 this.subscription = WebSocketService.Instance.subject
175 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
177 msg => this.parseMessage(msg),
178 err => console.error(err),
179 () => console.log('complete')
182 WebSocketService.Instance.getSite();
186 get isCurrentUser() {
188 UserService.Instance.user &&
189 UserService.Instance.user.id == this.state.user.id
193 static getViewFromProps(view: string): UserDetailsView {
194 return view ? UserDetailsView[view] : UserDetailsView.Overview;
197 static getSortTypeFromProps(sort: string): SortType {
198 return sort ? routeSortTypeToEnum(sort) : SortType.New;
201 static getPageFromProps(page: number): number {
202 return page ? Number(page) : 1;
205 componentWillUnmount() {
206 this.subscription.unsubscribe();
209 static getDerivedStateFromProps(props: any): UserProps {
211 view: this.getViewFromProps(props.match.params.view),
212 sort: this.getSortTypeFromProps(props.match.params.sort),
213 page: this.getPageFromProps(props.match.params.page),
214 user_id: Number(props.match.params.id) || null,
215 username: props.match.params.username,
219 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
220 // Necessary if you are on a post and you click another post (same route)
222 lastProps.location.pathname.split('/')[2] !==
223 lastProps.history.location.pathname.split('/')[2]
225 // Couldnt get a refresh working. This does for now.
230 get documentTitle(): string {
231 if (this.state.siteRes.site.name) {
232 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
238 get favIcon(): string {
239 return this.state.siteRes.site.icon
240 ? this.state.siteRes.site.icon
246 <div class="container">
247 <Helmet title={this.documentTitle}>
256 <div class="col-12 col-md-8">
257 {this.state.loading ? (
259 <svg class="icon icon-spinner spin">
260 <use xlinkHref="#icon-spinner"></use>
269 {!this.state.loading && this.selects()}
271 user_id={this.state.user_id}
272 username={this.state.username}
273 sort={this.state.sort}
274 page={this.state.page}
276 enableDownvotes={this.state.siteRes.site.enable_downvotes}
277 enableNsfw={this.state.siteRes.site.enable_nsfw}
278 admins={this.state.siteRes.admins}
279 view={this.state.view}
280 onPageChange={this.handlePageChange}
284 {!this.state.loading && (
285 <div class="col-12 col-md-4">
286 {this.isCurrentUser && this.userSettings()}
298 <div class="btn-group btn-group-toggle flex-wrap mb-2">
300 className={`btn btn-outline-secondary pointer
301 ${this.state.view == UserDetailsView.Overview && 'active'}
306 value={UserDetailsView.Overview}
307 checked={this.state.view === UserDetailsView.Overview}
308 onChange={linkEvent(this, this.handleViewChange)}
313 className={`btn btn-outline-secondary pointer
314 ${this.state.view == UserDetailsView.Comments && 'active'}
319 value={UserDetailsView.Comments}
320 checked={this.state.view == UserDetailsView.Comments}
321 onChange={linkEvent(this, this.handleViewChange)}
326 className={`btn btn-outline-secondary pointer
327 ${this.state.view == UserDetailsView.Posts && 'active'}
332 value={UserDetailsView.Posts}
333 checked={this.state.view == UserDetailsView.Posts}
334 onChange={linkEvent(this, this.handleViewChange)}
339 className={`btn btn-outline-secondary pointer
340 ${this.state.view == UserDetailsView.Saved && 'active'}
345 value={UserDetailsView.Saved}
346 checked={this.state.view == UserDetailsView.Saved}
347 onChange={linkEvent(this, this.handleViewChange)}
357 <div className="mb-2">
358 <span class="mr-3">{this.viewRadios()}</span>
360 sort={this.state.sort}
361 onChange={this.handleSortChange}
365 href={`/feeds/u/${this.state.username}.xml?sort=${this.state.sort}`}
370 <svg class="icon mx-2 text-muted small">
371 <use xlinkHref="#icon-rss">#</use>
379 let user = this.state.user;
384 banner={this.state.user.banner}
385 icon={this.state.user.avatar}
389 <div class="mb-0 d-flex flex-wrap">
391 {user.preferred_username && (
392 <h5 class="mb-0">{user.preferred_username}</h5>
394 <ul class="list-inline mb-2">
395 <li className="list-inline-item">
405 <li className="list-inline-item badge badge-danger">
411 <div className="flex-grow-1 unselectable pointer mx-2"></div>
412 {this.isCurrentUser ? (
414 class="d-flex align-self-start btn btn-secondary ml-2"
415 onClick={linkEvent(this, this.handleLogoutClick)}
422 className={`d-flex align-self-start btn btn-secondary ml-2 ${
423 !this.state.user.matrix_user_id && 'invisible'
427 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
429 {i18n.t('send_secure_message')}
432 class="d-flex align-self-start btn btn-secondary ml-2"
433 to={`/create_private_message?recipient_id=${this.state.user.id}`}
435 {i18n.t('send_message')}
441 <div className="d-flex align-items-center mb-2">
444 dangerouslySetInnerHTML={mdToHtml(user.bio)}
449 <ul class="list-inline mb-2">
450 <li className="list-inline-item badge badge-light">
451 {i18n.t('number_of_posts', { count: user.number_of_posts })}
453 <li className="list-inline-item badge badge-light">
454 {i18n.t('number_of_comments', {
455 count: user.number_of_comments,
460 <div class="text-muted">
461 {i18n.t('joined')} <MomentTime data={user} showAgo />
463 <div className="d-flex align-items-center text-muted mb-2">
465 <use xlinkHref="#icon-cake"></use>
467 <span className="ml-2">
468 {i18n.t('cake_day_title')}{' '}
469 {moment.utc(user.published).local().format('MMM DD, YYYY')}
481 <div class="card bg-transparent border-secondary mb-3">
482 <div class="card-body">
483 <h5>{i18n.t('settings')}</h5>
484 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
485 <div class="form-group">
486 <label>{i18n.t('avatar')}</label>
488 uploadTitle={i18n.t('upload_avatar')}
489 imageSrc={this.state.userSettingsForm.avatar}
490 onUpload={this.handleAvatarUpload}
491 onRemove={this.handleAvatarRemove}
495 <div class="form-group">
496 <label>{i18n.t('banner')}</label>
498 uploadTitle={i18n.t('upload_banner')}
499 imageSrc={this.state.userSettingsForm.banner}
500 onUpload={this.handleBannerUpload}
501 onRemove={this.handleBannerRemove}
504 <div class="form-group">
505 <label>{i18n.t('language')}</label>
507 value={this.state.userSettingsForm.lang}
508 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
509 class="ml-2 custom-select w-auto"
511 <option disabled>{i18n.t('language')}</option>
512 <option value="browser">{i18n.t('browser_default')}</option>
513 <option disabled>──</option>
514 {languages.map(lang => (
515 <option value={lang.code}>{lang.name}</option>
519 <div class="form-group">
520 <label>{i18n.t('theme')}</label>
522 value={this.state.userSettingsForm.theme}
523 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
524 class="ml-2 custom-select w-auto"
526 <option disabled>{i18n.t('theme')}</option>
527 {themes.map(theme => (
528 <option value={theme}>{theme}</option>
532 <form className="form-group">
534 <div class="mr-2">{i18n.t('sort_type')}</div>
538 Object.values(ListingType)[
539 this.state.userSettingsForm.default_listing_type
542 onChange={this.handleUserSettingsListingTypeChange}
545 <form className="form-group">
547 <div class="mr-2">{i18n.t('type')}</div>
551 Object.values(SortType)[
552 this.state.userSettingsForm.default_sort_type
555 onChange={this.handleUserSettingsSortTypeChange}
558 <div class="form-group row">
559 <label class="col-lg-5 col-form-label">
560 {i18n.t('display_name')}
562 <div class="col-lg-7">
566 placeholder={i18n.t('optional')}
567 value={this.state.userSettingsForm.preferred_username}
570 this.handleUserSettingsPreferredUsernameChange
572 pattern="^(?!@)(.+)$"
578 <div class="form-group row">
579 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
582 <div class="col-lg-9">
584 initialContent={this.state.userSettingsForm.bio}
585 onContentChange={this.handleUserSettingsBioChange}
587 hideNavigationWarnings
591 <div class="form-group row">
592 <label class="col-lg-3 col-form-label" htmlFor="user-email">
595 <div class="col-lg-9">
600 placeholder={i18n.t('optional')}
601 value={this.state.userSettingsForm.email}
604 this.handleUserSettingsEmailChange
610 <div class="form-group row">
611 <label class="col-lg-5 col-form-label">
612 <a href={elementUrl} target="_blank" rel="noopener">
613 {i18n.t('matrix_user_id')}
616 <div class="col-lg-7">
620 placeholder="@user:example.com"
621 value={this.state.userSettingsForm.matrix_user_id}
624 this.handleUserSettingsMatrixUserIdChange
630 <div class="form-group row">
631 <label class="col-lg-5 col-form-label" htmlFor="user-password">
632 {i18n.t('new_password')}
634 <div class="col-lg-7">
639 value={this.state.userSettingsForm.new_password}
640 autoComplete="new-password"
643 this.handleUserSettingsNewPasswordChange
648 <div class="form-group row">
650 class="col-lg-5 col-form-label"
651 htmlFor="user-verify-password"
653 {i18n.t('verify_password')}
655 <div class="col-lg-7">
658 id="user-verify-password"
660 value={this.state.userSettingsForm.new_password_verify}
661 autoComplete="new-password"
664 this.handleUserSettingsNewPasswordVerifyChange
669 <div class="form-group row">
671 class="col-lg-5 col-form-label"
672 htmlFor="user-old-password"
674 {i18n.t('old_password')}
676 <div class="col-lg-7">
679 id="user-old-password"
681 value={this.state.userSettingsForm.old_password}
682 autoComplete="new-password"
685 this.handleUserSettingsOldPasswordChange
690 {this.state.siteRes.site.enable_nsfw && (
691 <div class="form-group">
692 <div class="form-check">
694 class="form-check-input"
697 checked={this.state.userSettingsForm.show_nsfw}
700 this.handleUserSettingsShowNsfwChange
703 <label class="form-check-label" htmlFor="user-show-nsfw">
704 {i18n.t('show_nsfw')}
709 <div class="form-group">
710 <div class="form-check">
712 class="form-check-input"
713 id="user-show-avatars"
715 checked={this.state.userSettingsForm.show_avatars}
718 this.handleUserSettingsShowAvatarsChange
721 <label class="form-check-label" htmlFor="user-show-avatars">
722 {i18n.t('show_avatars')}
726 <div class="form-group">
727 <div class="form-check">
729 class="form-check-input"
730 id="user-send-notifications-to-email"
732 disabled={!this.state.userSettingsForm.email}
734 this.state.userSettingsForm.send_notifications_to_email
738 this.handleUserSettingsSendNotificationsToEmailChange
742 class="form-check-label"
743 htmlFor="user-send-notifications-to-email"
745 {i18n.t('send_notifications_to_email')}
749 <div class="form-group">
750 <button type="submit" class="btn btn-block btn-secondary mr-4">
751 {this.state.userSettingsLoading ? (
752 <svg class="icon icon-spinner spin">
753 <use xlinkHref="#icon-spinner"></use>
756 capitalizeFirstLetter(i18n.t('save'))
761 <div class="form-group mb-0">
763 class="btn btn-block btn-danger"
766 this.handleDeleteAccountShowConfirmToggle
769 {i18n.t('delete_account')}
771 {this.state.deleteAccountShowConfirm && (
773 <div class="my-2 alert alert-danger" role="alert">
774 {i18n.t('delete_account_confirm')}
778 value={this.state.deleteAccountForm.password}
779 autoComplete="new-password"
782 this.handleDeleteAccountPasswordChange
784 class="form-control my-2"
787 class="btn btn-danger mr-4"
788 disabled={!this.state.deleteAccountForm.password}
789 onClick={linkEvent(this, this.handleDeleteAccount)}
791 {this.state.deleteAccountLoading ? (
792 <svg class="icon icon-spinner spin">
793 <use xlinkHref="#icon-spinner"></use>
796 capitalizeFirstLetter(i18n.t('delete'))
800 class="btn btn-secondary"
803 this.handleDeleteAccountShowConfirmToggle
821 {this.state.moderates.length > 0 && (
822 <div class="card bg-transparent border-secondary mb-3">
823 <div class="card-body">
824 <h5>{i18n.t('moderates')}</h5>
825 <ul class="list-unstyled mb-0">
826 {this.state.moderates.map(community => (
828 <Link to={`/c/${community.community_name}`}>
829 {community.community_name}
844 {this.state.follows.length > 0 && (
845 <div class="card bg-transparent border-secondary mb-3">
846 <div class="card-body">
847 <h5>{i18n.t('subscribed')}</h5>
848 <ul class="list-unstyled mb-0">
849 {this.state.follows.map(community => (
851 <Link to={`/c/${community.community_name}`}>
852 {community.community_name}
864 updateUrl(paramUpdates: UrlParams) {
865 const page = paramUpdates.page || this.state.page;
866 const viewStr = paramUpdates.view || UserDetailsView[this.state.view];
867 const sortStr = paramUpdates.sort || this.state.sort;
868 this.props.history.push(
869 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
873 handlePageChange(page: number) {
874 this.updateUrl({ page });
877 handleSortChange(val: SortType) {
878 this.updateUrl({ sort: val, page: 1 });
881 handleViewChange(i: User, event: any) {
883 view: UserDetailsView[Number(event.target.value)],
888 handleUserSettingsShowNsfwChange(i: User, event: any) {
889 i.state.userSettingsForm.show_nsfw = event.target.checked;
893 handleUserSettingsShowAvatarsChange(i: User, event: any) {
894 i.state.userSettingsForm.show_avatars = event.target.checked;
895 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
899 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
900 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
904 handleUserSettingsThemeChange(i: User, event: any) {
905 i.state.userSettingsForm.theme = event.target.value;
906 setTheme(event.target.value, true);
910 handleUserSettingsLangChange(i: User, event: any) {
911 i.state.userSettingsForm.lang = event.target.value;
912 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
916 handleUserSettingsSortTypeChange(val: SortType) {
917 this.state.userSettingsForm.default_sort_type = Object.keys(
920 this.setState(this.state);
923 handleUserSettingsListingTypeChange(val: ListingType) {
924 this.state.userSettingsForm.default_listing_type = Object.keys(
927 this.setState(this.state);
930 handleUserSettingsEmailChange(i: User, event: any) {
931 i.state.userSettingsForm.email = event.target.value;
935 handleUserSettingsBioChange(val: string) {
936 this.state.userSettingsForm.bio = val;
937 this.setState(this.state);
940 handleAvatarUpload(url: string) {
941 this.state.userSettingsForm.avatar = url;
942 this.setState(this.state);
945 handleAvatarRemove() {
946 this.state.userSettingsForm.avatar = '';
947 this.setState(this.state);
950 handleBannerUpload(url: string) {
951 this.state.userSettingsForm.banner = url;
952 this.setState(this.state);
955 handleBannerRemove() {
956 this.state.userSettingsForm.banner = '';
957 this.setState(this.state);
960 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
961 i.state.userSettingsForm.preferred_username = event.target.value;
965 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
966 i.state.userSettingsForm.matrix_user_id = event.target.value;
968 i.state.userSettingsForm.matrix_user_id == '' &&
969 !i.state.user.matrix_user_id
971 i.state.userSettingsForm.matrix_user_id = undefined;
976 handleUserSettingsNewPasswordChange(i: User, event: any) {
977 i.state.userSettingsForm.new_password = event.target.value;
978 if (i.state.userSettingsForm.new_password == '') {
979 i.state.userSettingsForm.new_password = undefined;
984 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
985 i.state.userSettingsForm.new_password_verify = event.target.value;
986 if (i.state.userSettingsForm.new_password_verify == '') {
987 i.state.userSettingsForm.new_password_verify = undefined;
992 handleUserSettingsOldPasswordChange(i: User, event: any) {
993 i.state.userSettingsForm.old_password = event.target.value;
994 if (i.state.userSettingsForm.old_password == '') {
995 i.state.userSettingsForm.old_password = undefined;
1000 handleUserSettingsSubmit(i: User, event: any) {
1001 event.preventDefault();
1002 i.state.userSettingsLoading = true;
1003 i.setState(i.state);
1005 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1008 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1009 event.preventDefault();
1010 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1011 i.setState(i.state);
1014 handleDeleteAccountPasswordChange(i: User, event: any) {
1015 i.state.deleteAccountForm.password = event.target.value;
1016 i.setState(i.state);
1019 handleLogoutClick(i: User) {
1020 UserService.Instance.logout();
1021 i.context.router.history.push('/');
1024 handleDeleteAccount(i: User, event: any) {
1025 event.preventDefault();
1026 i.state.deleteAccountLoading = true;
1027 i.setState(i.state);
1029 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1032 parseMessage(msg: WebSocketJsonResponse) {
1034 const res = wsJsonToRes(msg);
1036 toast(i18n.t(msg.error), 'danger');
1037 if (msg.error == 'couldnt_find_that_username_or_email') {
1038 this.context.router.history.push('/');
1041 deleteAccountLoading: false,
1042 userSettingsLoading: false,
1045 } else if (res.op == UserOperation.GetUserDetails) {
1046 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1047 // and set the parent state if it is not set or differs
1048 const data = res.data as UserDetailsResponse;
1050 if (this.state.user.id !== data.user.id) {
1051 this.state.user = data.user;
1052 this.state.follows = data.follows;
1053 this.state.moderates = data.moderates;
1055 if (this.isCurrentUser) {
1056 this.state.userSettingsForm.show_nsfw =
1057 UserService.Instance.user.show_nsfw;
1058 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1059 ? UserService.Instance.user.theme
1061 this.state.userSettingsForm.default_sort_type =
1062 UserService.Instance.user.default_sort_type;
1063 this.state.userSettingsForm.default_listing_type =
1064 UserService.Instance.user.default_listing_type;
1065 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1066 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1067 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1068 this.state.userSettingsForm.preferred_username =
1069 UserService.Instance.user.preferred_username;
1070 this.state.userSettingsForm.show_avatars =
1071 UserService.Instance.user.show_avatars;
1072 this.state.userSettingsForm.email = UserService.Instance.user.email;
1073 this.state.userSettingsForm.bio = UserService.Instance.user.bio;
1074 this.state.userSettingsForm.send_notifications_to_email =
1075 UserService.Instance.user.send_notifications_to_email;
1076 this.state.userSettingsForm.matrix_user_id =
1077 UserService.Instance.user.matrix_user_id;
1079 this.state.loading = false;
1080 this.setState(this.state);
1082 } else if (res.op == UserOperation.SaveUserSettings) {
1083 const data = res.data as LoginResponse;
1084 UserService.Instance.login(data);
1085 this.state.user.bio = this.state.userSettingsForm.bio;
1086 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1087 this.state.user.banner = this.state.userSettingsForm.banner;
1088 this.state.user.avatar = this.state.userSettingsForm.avatar;
1089 this.state.userSettingsLoading = false;
1090 this.setState(this.state);
1092 window.scrollTo(0, 0);
1093 } else if (res.op == UserOperation.DeleteAccount) {
1095 deleteAccountLoading: false,
1096 deleteAccountShowConfirm: false,
1098 this.context.router.history.push('/');
1099 } else if (res.op == UserOperation.GetSite) {
1100 const data = res.data as GetSiteResponse;
1101 this.state.siteRes = data;
1102 this.setState(this.state);
1103 } else if (res.op == UserOperation.AddAdmin) {
1104 const data = res.data as AddAdminResponse;
1105 this.state.siteRes.admins = data.admins;
1106 this.setState(this.state);