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: Array<CommunityUser>;
53 moderates: Array<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
543 this.state.siteRes.federated_instances &&
544 this.state.siteRes.federated_instances.length > 0
546 onChange={this.handleUserSettingsListingTypeChange}
549 <form className="form-group">
551 <div class="mr-2">{i18n.t('type')}</div>
555 Object.values(SortType)[
556 this.state.userSettingsForm.default_sort_type
559 onChange={this.handleUserSettingsSortTypeChange}
562 <div class="form-group row">
563 <label class="col-lg-5 col-form-label">
564 {i18n.t('display_name')}
566 <div class="col-lg-7">
570 placeholder={i18n.t('optional')}
571 value={this.state.userSettingsForm.preferred_username}
574 this.handleUserSettingsPreferredUsernameChange
576 pattern="^(?!@)(.+)$"
582 <div class="form-group row">
583 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
586 <div class="col-lg-9">
588 initialContent={this.state.userSettingsForm.bio}
589 onContentChange={this.handleUserSettingsBioChange}
591 hideNavigationWarnings
595 <div class="form-group row">
596 <label class="col-lg-3 col-form-label" htmlFor="user-email">
599 <div class="col-lg-9">
604 placeholder={i18n.t('optional')}
605 value={this.state.userSettingsForm.email}
608 this.handleUserSettingsEmailChange
614 <div class="form-group row">
615 <label class="col-lg-5 col-form-label">
616 <a href={elementUrl} target="_blank" rel="noopener">
617 {i18n.t('matrix_user_id')}
620 <div class="col-lg-7">
624 placeholder="@user:example.com"
625 value={this.state.userSettingsForm.matrix_user_id}
628 this.handleUserSettingsMatrixUserIdChange
634 <div class="form-group row">
635 <label class="col-lg-5 col-form-label" htmlFor="user-password">
636 {i18n.t('new_password')}
638 <div class="col-lg-7">
643 value={this.state.userSettingsForm.new_password}
644 autoComplete="new-password"
647 this.handleUserSettingsNewPasswordChange
652 <div class="form-group row">
654 class="col-lg-5 col-form-label"
655 htmlFor="user-verify-password"
657 {i18n.t('verify_password')}
659 <div class="col-lg-7">
662 id="user-verify-password"
664 value={this.state.userSettingsForm.new_password_verify}
665 autoComplete="new-password"
668 this.handleUserSettingsNewPasswordVerifyChange
673 <div class="form-group row">
675 class="col-lg-5 col-form-label"
676 htmlFor="user-old-password"
678 {i18n.t('old_password')}
680 <div class="col-lg-7">
683 id="user-old-password"
685 value={this.state.userSettingsForm.old_password}
686 autoComplete="new-password"
689 this.handleUserSettingsOldPasswordChange
694 {this.state.siteRes.site.enable_nsfw && (
695 <div class="form-group">
696 <div class="form-check">
698 class="form-check-input"
701 checked={this.state.userSettingsForm.show_nsfw}
704 this.handleUserSettingsShowNsfwChange
707 <label class="form-check-label" htmlFor="user-show-nsfw">
708 {i18n.t('show_nsfw')}
713 <div class="form-group">
714 <div class="form-check">
716 class="form-check-input"
717 id="user-show-avatars"
719 checked={this.state.userSettingsForm.show_avatars}
722 this.handleUserSettingsShowAvatarsChange
725 <label class="form-check-label" htmlFor="user-show-avatars">
726 {i18n.t('show_avatars')}
730 <div class="form-group">
731 <div class="form-check">
733 class="form-check-input"
734 id="user-send-notifications-to-email"
736 disabled={!this.state.userSettingsForm.email}
738 this.state.userSettingsForm.send_notifications_to_email
742 this.handleUserSettingsSendNotificationsToEmailChange
746 class="form-check-label"
747 htmlFor="user-send-notifications-to-email"
749 {i18n.t('send_notifications_to_email')}
753 <div class="form-group">
754 <button type="submit" class="btn btn-block btn-secondary mr-4">
755 {this.state.userSettingsLoading ? (
756 <svg class="icon icon-spinner spin">
757 <use xlinkHref="#icon-spinner"></use>
760 capitalizeFirstLetter(i18n.t('save'))
765 <div class="form-group mb-0">
767 class="btn btn-block btn-danger"
770 this.handleDeleteAccountShowConfirmToggle
773 {i18n.t('delete_account')}
775 {this.state.deleteAccountShowConfirm && (
777 <div class="my-2 alert alert-danger" role="alert">
778 {i18n.t('delete_account_confirm')}
782 value={this.state.deleteAccountForm.password}
783 autoComplete="new-password"
786 this.handleDeleteAccountPasswordChange
788 class="form-control my-2"
791 class="btn btn-danger mr-4"
792 disabled={!this.state.deleteAccountForm.password}
793 onClick={linkEvent(this, this.handleDeleteAccount)}
795 {this.state.deleteAccountLoading ? (
796 <svg class="icon icon-spinner spin">
797 <use xlinkHref="#icon-spinner"></use>
800 capitalizeFirstLetter(i18n.t('delete'))
804 class="btn btn-secondary"
807 this.handleDeleteAccountShowConfirmToggle
825 {this.state.moderates.length > 0 && (
826 <div class="card bg-transparent border-secondary mb-3">
827 <div class="card-body">
828 <h5>{i18n.t('moderates')}</h5>
829 <ul class="list-unstyled mb-0">
830 {this.state.moderates.map(community => (
832 <Link to={`/c/${community.community_name}`}>
833 {community.community_name}
848 {this.state.follows.length > 0 && (
849 <div class="card bg-transparent border-secondary mb-3">
850 <div class="card-body">
851 <h5>{i18n.t('subscribed')}</h5>
852 <ul class="list-unstyled mb-0">
853 {this.state.follows.map(community => (
855 <Link to={`/c/${community.community_name}`}>
856 {community.community_name}
868 updateUrl(paramUpdates: UrlParams) {
869 const page = paramUpdates.page || this.state.page;
870 const viewStr = paramUpdates.view || UserDetailsView[this.state.view];
871 const sortStr = paramUpdates.sort || this.state.sort;
872 this.props.history.push(
873 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
877 handlePageChange(page: number) {
878 this.updateUrl({ page });
881 handleSortChange(val: SortType) {
882 this.updateUrl({ sort: val, page: 1 });
885 handleViewChange(i: User, event: any) {
887 view: UserDetailsView[Number(event.target.value)],
892 handleUserSettingsShowNsfwChange(i: User, event: any) {
893 i.state.userSettingsForm.show_nsfw = event.target.checked;
897 handleUserSettingsShowAvatarsChange(i: User, event: any) {
898 i.state.userSettingsForm.show_avatars = event.target.checked;
899 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
903 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
904 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
908 handleUserSettingsThemeChange(i: User, event: any) {
909 i.state.userSettingsForm.theme = event.target.value;
910 setTheme(event.target.value, true);
914 handleUserSettingsLangChange(i: User, event: any) {
915 i.state.userSettingsForm.lang = event.target.value;
916 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
920 handleUserSettingsSortTypeChange(val: SortType) {
921 this.state.userSettingsForm.default_sort_type = Object.keys(
924 this.setState(this.state);
927 handleUserSettingsListingTypeChange(val: ListingType) {
928 this.state.userSettingsForm.default_listing_type = Object.keys(
931 this.setState(this.state);
934 handleUserSettingsEmailChange(i: User, event: any) {
935 i.state.userSettingsForm.email = event.target.value;
939 handleUserSettingsBioChange(val: string) {
940 this.state.userSettingsForm.bio = val;
941 this.setState(this.state);
944 handleAvatarUpload(url: string) {
945 this.state.userSettingsForm.avatar = url;
946 this.setState(this.state);
949 handleAvatarRemove() {
950 this.state.userSettingsForm.avatar = '';
951 this.setState(this.state);
954 handleBannerUpload(url: string) {
955 this.state.userSettingsForm.banner = url;
956 this.setState(this.state);
959 handleBannerRemove() {
960 this.state.userSettingsForm.banner = '';
961 this.setState(this.state);
964 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
965 i.state.userSettingsForm.preferred_username = event.target.value;
969 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
970 i.state.userSettingsForm.matrix_user_id = event.target.value;
972 i.state.userSettingsForm.matrix_user_id == '' &&
973 !i.state.user.matrix_user_id
975 i.state.userSettingsForm.matrix_user_id = undefined;
980 handleUserSettingsNewPasswordChange(i: User, event: any) {
981 i.state.userSettingsForm.new_password = event.target.value;
982 if (i.state.userSettingsForm.new_password == '') {
983 i.state.userSettingsForm.new_password = undefined;
988 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
989 i.state.userSettingsForm.new_password_verify = event.target.value;
990 if (i.state.userSettingsForm.new_password_verify == '') {
991 i.state.userSettingsForm.new_password_verify = undefined;
996 handleUserSettingsOldPasswordChange(i: User, event: any) {
997 i.state.userSettingsForm.old_password = event.target.value;
998 if (i.state.userSettingsForm.old_password == '') {
999 i.state.userSettingsForm.old_password = undefined;
1001 i.setState(i.state);
1004 handleUserSettingsSubmit(i: User, event: any) {
1005 event.preventDefault();
1006 i.state.userSettingsLoading = true;
1007 i.setState(i.state);
1009 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1012 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1013 event.preventDefault();
1014 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1015 i.setState(i.state);
1018 handleDeleteAccountPasswordChange(i: User, event: any) {
1019 i.state.deleteAccountForm.password = event.target.value;
1020 i.setState(i.state);
1023 handleLogoutClick(i: User) {
1024 UserService.Instance.logout();
1025 i.context.router.history.push('/');
1028 handleDeleteAccount(i: User, event: any) {
1029 event.preventDefault();
1030 i.state.deleteAccountLoading = true;
1031 i.setState(i.state);
1033 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1036 parseMessage(msg: WebSocketJsonResponse) {
1038 const res = wsJsonToRes(msg);
1040 toast(i18n.t(msg.error), 'danger');
1041 if (msg.error == 'couldnt_find_that_username_or_email') {
1042 this.context.router.history.push('/');
1045 deleteAccountLoading: false,
1046 userSettingsLoading: false,
1049 } else if (res.op == UserOperation.GetUserDetails) {
1050 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1051 // and set the parent state if it is not set or differs
1052 const data = res.data as UserDetailsResponse;
1054 if (this.state.user.id !== data.user.id) {
1055 this.state.user = data.user;
1056 this.state.follows = data.follows;
1057 this.state.moderates = data.moderates;
1059 if (this.isCurrentUser) {
1060 this.state.userSettingsForm.show_nsfw =
1061 UserService.Instance.user.show_nsfw;
1062 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1063 ? UserService.Instance.user.theme
1065 this.state.userSettingsForm.default_sort_type =
1066 UserService.Instance.user.default_sort_type;
1067 this.state.userSettingsForm.default_listing_type =
1068 UserService.Instance.user.default_listing_type;
1069 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1070 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1071 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1072 this.state.userSettingsForm.preferred_username =
1073 UserService.Instance.user.preferred_username;
1074 this.state.userSettingsForm.show_avatars =
1075 UserService.Instance.user.show_avatars;
1076 this.state.userSettingsForm.email = UserService.Instance.user.email;
1077 this.state.userSettingsForm.bio = UserService.Instance.user.bio;
1078 this.state.userSettingsForm.send_notifications_to_email =
1079 UserService.Instance.user.send_notifications_to_email;
1080 this.state.userSettingsForm.matrix_user_id =
1081 UserService.Instance.user.matrix_user_id;
1083 this.state.loading = false;
1084 this.setState(this.state);
1086 } else if (res.op == UserOperation.SaveUserSettings) {
1087 const data = res.data as LoginResponse;
1088 UserService.Instance.login(data);
1089 this.state.user.bio = this.state.userSettingsForm.bio;
1090 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1091 this.state.user.banner = this.state.userSettingsForm.banner;
1092 this.state.user.avatar = this.state.userSettingsForm.avatar;
1093 this.state.userSettingsLoading = false;
1094 this.setState(this.state);
1096 window.scrollTo(0, 0);
1097 } else if (res.op == UserOperation.DeleteAccount) {
1099 deleteAccountLoading: false,
1100 deleteAccountShowConfirm: false,
1102 this.context.router.history.push('/');
1103 } else if (res.op == UserOperation.GetSite) {
1104 const data = res.data as GetSiteResponse;
1105 this.state.siteRes = data;
1106 this.setState(this.state);
1107 } else if (res.op == UserOperation.AddAdmin) {
1108 const data = res.data as AddAdminResponse;
1109 this.state.siteRes.admins = data.admins;
1110 this.setState(this.state);