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,
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();
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.userSettingsForm.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;
928 handleUserSettingsBioChange(val: string) {
929 this.state.userSettingsForm.bio = val;
930 this.setState(this.state);
933 handleAvatarUpload(url: string) {
934 this.state.userSettingsForm.avatar = url;
935 this.setState(this.state);
938 handleAvatarRemove() {
939 this.state.userSettingsForm.avatar = '';
940 this.setState(this.state);
943 handleBannerUpload(url: string) {
944 this.state.userSettingsForm.banner = url;
945 this.setState(this.state);
948 handleBannerRemove() {
949 this.state.userSettingsForm.banner = '';
950 this.setState(this.state);
953 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
954 i.state.userSettingsForm.preferred_username = event.target.value;
958 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
959 i.state.userSettingsForm.matrix_user_id = event.target.value;
961 i.state.userSettingsForm.matrix_user_id == '' &&
962 !i.state.user.matrix_user_id
964 i.state.userSettingsForm.matrix_user_id = undefined;
969 handleUserSettingsNewPasswordChange(i: User, event: any) {
970 i.state.userSettingsForm.new_password = event.target.value;
971 if (i.state.userSettingsForm.new_password == '') {
972 i.state.userSettingsForm.new_password = undefined;
977 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
978 i.state.userSettingsForm.new_password_verify = event.target.value;
979 if (i.state.userSettingsForm.new_password_verify == '') {
980 i.state.userSettingsForm.new_password_verify = undefined;
985 handleUserSettingsOldPasswordChange(i: User, event: any) {
986 i.state.userSettingsForm.old_password = event.target.value;
987 if (i.state.userSettingsForm.old_password == '') {
988 i.state.userSettingsForm.old_password = undefined;
993 handleUserSettingsSubmit(i: User, event: any) {
994 event.preventDefault();
995 i.state.userSettingsLoading = true;
998 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1001 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1002 event.preventDefault();
1003 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1004 i.setState(i.state);
1007 handleDeleteAccountPasswordChange(i: User, event: any) {
1008 i.state.deleteAccountForm.password = event.target.value;
1009 i.setState(i.state);
1012 handleLogoutClick(i: User) {
1013 UserService.Instance.logout();
1014 i.context.router.history.push('/');
1017 handleDeleteAccount(i: User, event: any) {
1018 event.preventDefault();
1019 i.state.deleteAccountLoading = true;
1020 i.setState(i.state);
1022 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1025 parseMessage(msg: WebSocketJsonResponse) {
1027 const res = wsJsonToRes(msg);
1029 toast(i18n.t(msg.error), 'danger');
1030 if (msg.error == 'couldnt_find_that_username_or_email') {
1031 this.context.router.history.push('/');
1034 deleteAccountLoading: false,
1035 userSettingsLoading: false,
1038 } else if (res.op == UserOperation.GetUserDetails) {
1039 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1040 // and set the parent state if it is not set or differs
1041 const data = res.data as UserDetailsResponse;
1043 if (this.state.user.id !== data.user.id) {
1044 this.state.user = data.user;
1045 this.state.follows = data.follows;
1046 this.state.moderates = data.moderates;
1048 if (this.isCurrentUser) {
1049 this.state.userSettingsForm.show_nsfw =
1050 UserService.Instance.user.show_nsfw;
1051 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1052 ? UserService.Instance.user.theme
1054 this.state.userSettingsForm.default_sort_type =
1055 UserService.Instance.user.default_sort_type;
1056 this.state.userSettingsForm.default_listing_type =
1057 UserService.Instance.user.default_listing_type;
1058 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1059 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1060 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1061 this.state.userSettingsForm.preferred_username =
1062 UserService.Instance.user.preferred_username;
1063 this.state.userSettingsForm.show_avatars =
1064 UserService.Instance.user.show_avatars;
1065 this.state.userSettingsForm.email = UserService.Instance.user.email;
1066 this.state.userSettingsForm.bio = UserService.Instance.user.bio;
1067 this.state.userSettingsForm.send_notifications_to_email =
1068 UserService.Instance.user.send_notifications_to_email;
1069 this.state.userSettingsForm.matrix_user_id =
1070 UserService.Instance.user.matrix_user_id;
1072 this.state.loading = false;
1073 this.setState(this.state);
1075 } else if (res.op == UserOperation.SaveUserSettings) {
1076 const data = res.data as LoginResponse;
1077 UserService.Instance.login(data);
1078 this.state.user.bio = this.state.userSettingsForm.bio;
1079 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1080 this.state.user.banner = this.state.userSettingsForm.banner;
1081 this.state.user.avatar = this.state.userSettingsForm.avatar;
1082 this.state.userSettingsLoading = false;
1083 this.setState(this.state);
1085 window.scrollTo(0, 0);
1086 } else if (res.op == UserOperation.DeleteAccount) {
1088 deleteAccountLoading: false,
1089 deleteAccountShowConfirm: false,
1091 this.context.router.history.push('/');
1092 } else if (res.op == UserOperation.GetSite) {
1093 const data = res.data as GetSiteResponse;
1094 this.state.siteRes = data;
1095 this.setState(this.state);
1096 } else if (res.op == UserOperation.AddAdmin) {
1097 const data = res.data as AddAdminResponse;
1098 this.state.siteRes.admins = data.admins;
1099 this.setState(this.state);