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,
20 } 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,
94 send_notifications_to_email: null,
103 view: User.getViewFromProps(this.props.match.view),
104 sort: User.getSortTypeFromProps(this.props.match.sort),
105 page: User.getPageFromProps(this.props.match.page),
109 default_sort_type: null,
110 default_listing_type: null,
113 send_notifications_to_email: null,
116 preferred_username: null,
118 userSettingsLoading: null,
119 deleteAccountLoading: null,
120 deleteAccountShowConfirm: false,
131 creator_id: undefined,
132 published: undefined,
133 creator_name: undefined,
134 number_of_users: undefined,
135 number_of_posts: undefined,
136 number_of_comments: undefined,
137 number_of_communities: undefined,
138 enable_downvotes: undefined,
139 open_registration: undefined,
140 enable_nsfw: undefined,
143 creator_preferred_username: 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();
185 get isCurrentUser() {
187 UserService.Instance.user &&
188 UserService.Instance.user.id == this.state.user.id
192 static getViewFromProps(view: any): UserDetailsView {
194 ? UserDetailsView[capitalizeFirstLetter(view)]
195 : UserDetailsView.Overview;
198 static getSortTypeFromProps(sort: any): SortType {
199 return sort ? routeSortTypeToEnum(sort) : SortType.New;
202 static getPageFromProps(page: any): number {
203 return page ? Number(page) : 1;
206 componentWillUnmount() {
207 this.subscription.unsubscribe();
210 static getDerivedStateFromProps(props: any): UserProps {
212 view: this.getViewFromProps(props.match.params.view),
213 sort: this.getSortTypeFromProps(props.match.params.sort),
214 page: this.getPageFromProps(props.match.params.page),
215 user_id: Number(props.match.params.id) || null,
216 username: props.match.params.username,
220 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
221 // Necessary if you are on a post and you click another post (same route)
223 lastProps.location.pathname.split('/')[2] !==
224 lastProps.history.location.pathname.split('/')[2]
226 // Couldnt get a refresh working. This does for now.
232 get documentTitle(): string {
233 if (this.state.siteRes.site.name) {
234 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
240 get favIcon(): string {
241 return this.state.siteRes.site.icon
242 ? this.state.siteRes.site.icon
248 <div class="container">
249 <Helmet title={this.documentTitle}>
258 <div class="col-12 col-md-8">
259 {this.state.loading ? (
261 <svg class="icon icon-spinner spin">
262 <use xlinkHref="#icon-spinner"></use>
271 {!this.state.loading && this.selects()}
273 user_id={this.state.user_id}
274 username={this.state.username}
275 sort={SortType[this.state.sort]}
276 page={this.state.page}
278 enableDownvotes={this.state.siteRes.site.enable_downvotes}
279 enableNsfw={this.state.siteRes.site.enable_nsfw}
280 admins={this.state.siteRes.admins}
281 view={this.state.view}
282 onPageChange={this.handlePageChange}
286 {!this.state.loading && (
287 <div class="col-12 col-md-4">
288 {this.isCurrentUser && this.userSettings()}
300 <div class="btn-group btn-group-toggle flex-wrap mb-2">
302 className={`btn btn-outline-secondary pointer
303 ${this.state.view == UserDetailsView.Overview && 'active'}
308 value={UserDetailsView.Overview}
309 checked={this.state.view === UserDetailsView.Overview}
310 onChange={linkEvent(this, this.handleViewChange)}
315 className={`btn btn-outline-secondary pointer
316 ${this.state.view == UserDetailsView.Comments && 'active'}
321 value={UserDetailsView.Comments}
322 checked={this.state.view == UserDetailsView.Comments}
323 onChange={linkEvent(this, this.handleViewChange)}
328 className={`btn btn-outline-secondary pointer
329 ${this.state.view == UserDetailsView.Posts && 'active'}
334 value={UserDetailsView.Posts}
335 checked={this.state.view == UserDetailsView.Posts}
336 onChange={linkEvent(this, this.handleViewChange)}
341 className={`btn btn-outline-secondary pointer
342 ${this.state.view == UserDetailsView.Saved && 'active'}
347 value={UserDetailsView.Saved}
348 checked={this.state.view == UserDetailsView.Saved}
349 onChange={linkEvent(this, this.handleViewChange)}
359 <div className="mb-2">
360 <span class="mr-3">{this.viewRadios()}</span>
362 sort={this.state.sort}
363 onChange={this.handleSortChange}
367 href={`/feeds/u/${this.state.username}.xml?sort=${
368 SortType[this.state.sort]
374 <svg class="icon mx-2 text-muted small">
375 <use xlinkHref="#icon-rss">#</use>
383 let user = this.state.user;
388 banner={this.state.user.banner}
389 icon={this.state.user.avatar}
393 <div class="mb-0 d-flex flex-wrap">
395 {user.preferred_username && (
396 <h5 class="mb-0">{user.preferred_username}</h5>
398 <ul class="list-inline mb-2">
399 <li className="list-inline-item">
409 <li className="list-inline-item badge badge-danger">
415 <div className="flex-grow-1 unselectable pointer mx-2"></div>
416 {this.isCurrentUser ? (
418 class="d-flex align-self-start btn btn-secondary ml-2"
419 onClick={linkEvent(this, this.handleLogoutClick)}
426 className={`d-flex align-self-start btn btn-secondary ml-2 ${
427 !this.state.user.matrix_user_id && 'invisible'
431 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
433 {i18n.t('send_secure_message')}
436 class="d-flex align-self-start btn btn-secondary ml-2"
437 to={`/create_private_message?recipient_id=${this.state.user.id}`}
439 {i18n.t('send_message')}
445 <div className="d-flex align-items-center mb-2">
448 dangerouslySetInnerHTML={mdToHtml(user.bio)}
453 <ul class="list-inline mb-2">
454 <li className="list-inline-item badge badge-light">
455 {i18n.t('number_of_posts', { count: user.number_of_posts })}
457 <li className="list-inline-item badge badge-light">
458 {i18n.t('number_of_comments', {
459 count: user.number_of_comments,
464 <div class="text-muted">
465 {i18n.t('joined')} <MomentTime data={user} showAgo />
467 <div className="d-flex align-items-center text-muted mb-2">
469 <use xlinkHref="#icon-cake"></use>
471 <span className="ml-2">
472 {i18n.t('cake_day_title')}{' '}
473 {moment.utc(user.published).local().format('MMM DD, YYYY')}
485 <div class="card bg-transparent border-secondary mb-3">
486 <div class="card-body">
487 <h5>{i18n.t('settings')}</h5>
488 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
489 <div class="form-group">
490 <label>{i18n.t('avatar')}</label>
492 uploadTitle={i18n.t('upload_avatar')}
493 imageSrc={this.state.userSettingsForm.avatar}
494 onUpload={this.handleAvatarUpload}
495 onRemove={this.handleAvatarRemove}
499 <div class="form-group">
500 <label>{i18n.t('banner')}</label>
502 uploadTitle={i18n.t('upload_banner')}
503 imageSrc={this.state.userSettingsForm.banner}
504 onUpload={this.handleBannerUpload}
505 onRemove={this.handleBannerRemove}
508 <div class="form-group">
509 <label>{i18n.t('language')}</label>
511 value={this.state.userSettingsForm.lang}
512 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
513 class="ml-2 custom-select w-auto"
515 <option disabled>{i18n.t('language')}</option>
516 <option value="browser">{i18n.t('browser_default')}</option>
517 <option disabled>──</option>
518 {languages.map(lang => (
519 <option value={lang.code}>{lang.name}</option>
523 <div class="form-group">
524 <label>{i18n.t('theme')}</label>
526 value={this.state.userSettingsForm.theme}
527 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
528 class="ml-2 custom-select w-auto"
530 <option disabled>{i18n.t('theme')}</option>
531 {themes.map(theme => (
532 <option value={theme}>{theme}</option>
536 <form className="form-group">
538 <div class="mr-2">{i18n.t('sort_type')}</div>
541 type_={this.state.userSettingsForm.default_listing_type}
542 onChange={this.handleUserSettingsListingTypeChange}
545 <form className="form-group">
547 <div class="mr-2">{i18n.t('type')}</div>
550 sort={this.state.userSettingsForm.default_sort_type}
551 onChange={this.handleUserSettingsSortTypeChange}
554 <div class="form-group row">
555 <label class="col-lg-5 col-form-label">
556 {i18n.t('display_name')}
558 <div class="col-lg-7">
562 placeholder={i18n.t('optional')}
563 value={this.state.userSettingsForm.preferred_username}
566 this.handleUserSettingsPreferredUsernameChange
573 <div class="form-group row">
574 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
577 <div class="col-lg-9">
579 initialContent={this.state.userSettingsForm.bio}
580 onContentChange={this.handleUserSettingsBioChange}
582 hideNavigationWarnings
586 <div class="form-group row">
587 <label class="col-lg-3 col-form-label" htmlFor="user-email">
590 <div class="col-lg-9">
595 placeholder={i18n.t('optional')}
596 value={this.state.userSettingsForm.email}
599 this.handleUserSettingsEmailChange
605 <div class="form-group row">
606 <label class="col-lg-5 col-form-label">
607 <a href={elementUrl} target="_blank" rel="noopener">
608 {i18n.t('matrix_user_id')}
611 <div class="col-lg-7">
615 placeholder="@user:example.com"
616 value={this.state.userSettingsForm.matrix_user_id}
619 this.handleUserSettingsMatrixUserIdChange
625 <div class="form-group row">
626 <label class="col-lg-5 col-form-label" htmlFor="user-password">
627 {i18n.t('new_password')}
629 <div class="col-lg-7">
634 value={this.state.userSettingsForm.new_password}
635 autoComplete="new-password"
638 this.handleUserSettingsNewPasswordChange
643 <div class="form-group row">
645 class="col-lg-5 col-form-label"
646 htmlFor="user-verify-password"
648 {i18n.t('verify_password')}
650 <div class="col-lg-7">
653 id="user-verify-password"
655 value={this.state.userSettingsForm.new_password_verify}
656 autoComplete="new-password"
659 this.handleUserSettingsNewPasswordVerifyChange
664 <div class="form-group row">
666 class="col-lg-5 col-form-label"
667 htmlFor="user-old-password"
669 {i18n.t('old_password')}
671 <div class="col-lg-7">
674 id="user-old-password"
676 value={this.state.userSettingsForm.old_password}
677 autoComplete="new-password"
680 this.handleUserSettingsOldPasswordChange
685 {this.state.siteRes.site.enable_nsfw && (
686 <div class="form-group">
687 <div class="form-check">
689 class="form-check-input"
692 checked={this.state.userSettingsForm.show_nsfw}
695 this.handleUserSettingsShowNsfwChange
698 <label class="form-check-label" htmlFor="user-show-nsfw">
699 {i18n.t('show_nsfw')}
704 <div class="form-group">
705 <div class="form-check">
707 class="form-check-input"
708 id="user-show-avatars"
710 checked={this.state.userSettingsForm.show_avatars}
713 this.handleUserSettingsShowAvatarsChange
716 <label class="form-check-label" htmlFor="user-show-avatars">
717 {i18n.t('show_avatars')}
721 <div class="form-group">
722 <div class="form-check">
724 class="form-check-input"
725 id="user-send-notifications-to-email"
727 disabled={!this.state.user.email}
729 this.state.userSettingsForm.send_notifications_to_email
733 this.handleUserSettingsSendNotificationsToEmailChange
737 class="form-check-label"
738 htmlFor="user-send-notifications-to-email"
740 {i18n.t('send_notifications_to_email')}
744 <div class="form-group">
745 <button type="submit" class="btn btn-block btn-secondary mr-4">
746 {this.state.userSettingsLoading ? (
747 <svg class="icon icon-spinner spin">
748 <use xlinkHref="#icon-spinner"></use>
751 capitalizeFirstLetter(i18n.t('save'))
756 <div class="form-group mb-0">
758 class="btn btn-block btn-danger"
761 this.handleDeleteAccountShowConfirmToggle
764 {i18n.t('delete_account')}
766 {this.state.deleteAccountShowConfirm && (
768 <div class="my-2 alert alert-danger" role="alert">
769 {i18n.t('delete_account_confirm')}
773 value={this.state.deleteAccountForm.password}
774 autoComplete="new-password"
777 this.handleDeleteAccountPasswordChange
779 class="form-control my-2"
782 class="btn btn-danger mr-4"
783 disabled={!this.state.deleteAccountForm.password}
784 onClick={linkEvent(this, this.handleDeleteAccount)}
786 {this.state.deleteAccountLoading ? (
787 <svg class="icon icon-spinner spin">
788 <use xlinkHref="#icon-spinner"></use>
791 capitalizeFirstLetter(i18n.t('delete'))
795 class="btn btn-secondary"
798 this.handleDeleteAccountShowConfirmToggle
816 {this.state.moderates.length > 0 && (
817 <div class="card bg-transparent border-secondary mb-3">
818 <div class="card-body">
819 <h5>{i18n.t('moderates')}</h5>
820 <ul class="list-unstyled mb-0">
821 {this.state.moderates.map(community => (
823 <Link to={`/c/${community.community_name}`}>
824 {community.community_name}
839 {this.state.follows.length > 0 && (
840 <div class="card bg-transparent border-secondary mb-3">
841 <div class="card-body">
842 <h5>{i18n.t('subscribed')}</h5>
843 <ul class="list-unstyled mb-0">
844 {this.state.follows.map(community => (
846 <Link to={`/c/${community.community_name}`}>
847 {community.community_name}
859 updateUrl(paramUpdates: UrlParams) {
860 const page = paramUpdates.page || this.state.page;
862 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
864 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
865 this.props.history.push(
866 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
870 handlePageChange(page: number) {
871 this.updateUrl({ page });
874 handleSortChange(val: SortType) {
875 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
878 handleViewChange(i: User, event: any) {
880 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
885 handleUserSettingsShowNsfwChange(i: User, event: any) {
886 i.state.userSettingsForm.show_nsfw = event.target.checked;
890 handleUserSettingsShowAvatarsChange(i: User, event: any) {
891 i.state.userSettingsForm.show_avatars = event.target.checked;
892 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
896 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
897 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
901 handleUserSettingsThemeChange(i: User, event: any) {
902 i.state.userSettingsForm.theme = event.target.value;
903 setTheme(event.target.value, true);
907 handleUserSettingsLangChange(i: User, event: any) {
908 i.state.userSettingsForm.lang = event.target.value;
909 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
913 handleUserSettingsSortTypeChange(val: SortType) {
914 this.state.userSettingsForm.default_sort_type = val;
915 this.setState(this.state);
918 handleUserSettingsListingTypeChange(val: ListingType) {
919 this.state.userSettingsForm.default_listing_type = val;
920 this.setState(this.state);
923 handleUserSettingsEmailChange(i: User, event: any) {
924 i.state.userSettingsForm.email = event.target.value;
925 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
926 i.state.userSettingsForm.email = undefined;
931 handleUserSettingsBioChange(val: string) {
932 this.state.userSettingsForm.bio = val;
933 this.setState(this.state);
936 handleAvatarUpload(url: string) {
937 this.state.userSettingsForm.avatar = url;
938 this.setState(this.state);
941 handleAvatarRemove() {
942 this.state.userSettingsForm.avatar = '';
943 this.setState(this.state);
946 handleBannerUpload(url: string) {
947 this.state.userSettingsForm.banner = url;
948 this.setState(this.state);
951 handleBannerRemove() {
952 this.state.userSettingsForm.banner = '';
953 this.setState(this.state);
956 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
957 i.state.userSettingsForm.preferred_username = event.target.value;
961 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
962 i.state.userSettingsForm.matrix_user_id = event.target.value;
964 i.state.userSettingsForm.matrix_user_id == '' &&
965 !i.state.user.matrix_user_id
967 i.state.userSettingsForm.matrix_user_id = undefined;
972 handleUserSettingsNewPasswordChange(i: User, event: any) {
973 i.state.userSettingsForm.new_password = event.target.value;
974 if (i.state.userSettingsForm.new_password == '') {
975 i.state.userSettingsForm.new_password = undefined;
980 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
981 i.state.userSettingsForm.new_password_verify = event.target.value;
982 if (i.state.userSettingsForm.new_password_verify == '') {
983 i.state.userSettingsForm.new_password_verify = undefined;
988 handleUserSettingsOldPasswordChange(i: User, event: any) {
989 i.state.userSettingsForm.old_password = event.target.value;
990 if (i.state.userSettingsForm.old_password == '') {
991 i.state.userSettingsForm.old_password = undefined;
996 handleUserSettingsSubmit(i: User, event: any) {
997 event.preventDefault();
998 i.state.userSettingsLoading = true;
1001 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1004 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1005 event.preventDefault();
1006 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1007 i.setState(i.state);
1010 handleDeleteAccountPasswordChange(i: User, event: any) {
1011 i.state.deleteAccountForm.password = event.target.value;
1012 i.setState(i.state);
1015 handleLogoutClick(i: User) {
1016 UserService.Instance.logout();
1017 i.context.router.history.push('/');
1020 handleDeleteAccount(i: User, event: any) {
1021 event.preventDefault();
1022 i.state.deleteAccountLoading = true;
1023 i.setState(i.state);
1025 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1028 parseMessage(msg: WebSocketJsonResponse) {
1030 const res = wsJsonToRes(msg);
1032 toast(i18n.t(msg.error), 'danger');
1033 if (msg.error == 'couldnt_find_that_username_or_email') {
1034 this.context.router.history.push('/');
1037 deleteAccountLoading: false,
1038 userSettingsLoading: false,
1041 } else if (res.op == UserOperation.GetUserDetails) {
1042 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1043 // and set the parent state if it is not set or differs
1044 const data = res.data as UserDetailsResponse;
1046 if (this.state.user.id !== data.user.id) {
1047 this.state.user = data.user;
1048 this.state.follows = data.follows;
1049 this.state.moderates = data.moderates;
1051 if (this.isCurrentUser) {
1052 this.state.userSettingsForm.show_nsfw =
1053 UserService.Instance.user.show_nsfw;
1054 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1055 ? UserService.Instance.user.theme
1057 this.state.userSettingsForm.default_sort_type =
1058 UserService.Instance.user.default_sort_type;
1059 this.state.userSettingsForm.default_listing_type =
1060 UserService.Instance.user.default_listing_type;
1061 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1062 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1063 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1064 this.state.userSettingsForm.preferred_username =
1065 UserService.Instance.user.preferred_username;
1066 this.state.userSettingsForm.email = this.state.user.email;
1067 this.state.userSettingsForm.bio = this.state.user.bio;
1068 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1069 this.state.userSettingsForm.show_avatars =
1070 UserService.Instance.user.show_avatars;
1071 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1073 this.state.loading = false;
1074 this.setState(this.state);
1076 } else if (res.op == UserOperation.SaveUserSettings) {
1077 const data = res.data as LoginResponse;
1078 UserService.Instance.login(data);
1079 this.state.user.bio = this.state.userSettingsForm.bio;
1080 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1081 this.state.user.banner = this.state.userSettingsForm.banner;
1082 this.state.user.avatar = this.state.userSettingsForm.avatar;
1083 this.state.userSettingsLoading = false;
1084 this.setState(this.state);
1086 window.scrollTo(0, 0);
1087 } else if (res.op == UserOperation.DeleteAccount) {
1089 deleteAccountLoading: false,
1090 deleteAccountShowConfirm: false,
1092 this.context.router.history.push('/');
1093 } else if (res.op == UserOperation.GetSite) {
1094 const data = res.data as GetSiteResponse;
1095 this.state.siteRes = data;
1096 this.setState(this.state);
1097 } else if (res.op == UserOperation.AddAdmin) {
1098 const data = res.data as AddAdminResponse;
1099 this.state.siteRes.admins = data.admins;
1100 this.setState(this.state);