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();
186 get isCurrentUser() {
188 UserService.Instance.user &&
189 UserService.Instance.user.id == this.state.user.id
193 static getViewFromProps(view: any): UserDetailsView {
195 ? UserDetailsView[capitalizeFirstLetter(view)]
196 : UserDetailsView.Overview;
199 static getSortTypeFromProps(sort: any): SortType {
200 return sort ? routeSortTypeToEnum(sort) : SortType.New;
203 static getPageFromProps(page: any): number {
204 return page ? Number(page) : 1;
207 componentWillUnmount() {
208 this.subscription.unsubscribe();
211 static getDerivedStateFromProps(props: any): UserProps {
213 view: this.getViewFromProps(props.match.params.view),
214 sort: this.getSortTypeFromProps(props.match.params.sort),
215 page: this.getPageFromProps(props.match.params.page),
216 user_id: Number(props.match.params.id) || null,
217 username: props.match.params.username,
221 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
222 // Necessary if you are on a post and you click another post (same route)
224 lastProps.location.pathname.split('/')[2] !==
225 lastProps.history.location.pathname.split('/')[2]
227 // 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
568 pattern="^(?!@)(.+)$"
574 <div class="form-group row">
575 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
578 <div class="col-lg-9">
580 initialContent={this.state.userSettingsForm.bio}
581 onContentChange={this.handleUserSettingsBioChange}
583 hideNavigationWarnings
587 <div class="form-group row">
588 <label class="col-lg-3 col-form-label" htmlFor="user-email">
591 <div class="col-lg-9">
596 placeholder={i18n.t('optional')}
597 value={this.state.userSettingsForm.email}
600 this.handleUserSettingsEmailChange
606 <div class="form-group row">
607 <label class="col-lg-5 col-form-label">
608 <a href={elementUrl} target="_blank" rel="noopener">
609 {i18n.t('matrix_user_id')}
612 <div class="col-lg-7">
616 placeholder="@user:example.com"
617 value={this.state.userSettingsForm.matrix_user_id}
620 this.handleUserSettingsMatrixUserIdChange
626 <div class="form-group row">
627 <label class="col-lg-5 col-form-label" htmlFor="user-password">
628 {i18n.t('new_password')}
630 <div class="col-lg-7">
635 value={this.state.userSettingsForm.new_password}
636 autoComplete="new-password"
639 this.handleUserSettingsNewPasswordChange
644 <div class="form-group row">
646 class="col-lg-5 col-form-label"
647 htmlFor="user-verify-password"
649 {i18n.t('verify_password')}
651 <div class="col-lg-7">
654 id="user-verify-password"
656 value={this.state.userSettingsForm.new_password_verify}
657 autoComplete="new-password"
660 this.handleUserSettingsNewPasswordVerifyChange
665 <div class="form-group row">
667 class="col-lg-5 col-form-label"
668 htmlFor="user-old-password"
670 {i18n.t('old_password')}
672 <div class="col-lg-7">
675 id="user-old-password"
677 value={this.state.userSettingsForm.old_password}
678 autoComplete="new-password"
681 this.handleUserSettingsOldPasswordChange
686 {this.state.siteRes.site.enable_nsfw && (
687 <div class="form-group">
688 <div class="form-check">
690 class="form-check-input"
693 checked={this.state.userSettingsForm.show_nsfw}
696 this.handleUserSettingsShowNsfwChange
699 <label class="form-check-label" htmlFor="user-show-nsfw">
700 {i18n.t('show_nsfw')}
705 <div class="form-group">
706 <div class="form-check">
708 class="form-check-input"
709 id="user-show-avatars"
711 checked={this.state.userSettingsForm.show_avatars}
714 this.handleUserSettingsShowAvatarsChange
717 <label class="form-check-label" htmlFor="user-show-avatars">
718 {i18n.t('show_avatars')}
722 <div class="form-group">
723 <div class="form-check">
725 class="form-check-input"
726 id="user-send-notifications-to-email"
728 disabled={!this.state.userSettingsForm.email}
730 this.state.userSettingsForm.send_notifications_to_email
734 this.handleUserSettingsSendNotificationsToEmailChange
738 class="form-check-label"
739 htmlFor="user-send-notifications-to-email"
741 {i18n.t('send_notifications_to_email')}
745 <div class="form-group">
746 <button type="submit" class="btn btn-block btn-secondary mr-4">
747 {this.state.userSettingsLoading ? (
748 <svg class="icon icon-spinner spin">
749 <use xlinkHref="#icon-spinner"></use>
752 capitalizeFirstLetter(i18n.t('save'))
757 <div class="form-group mb-0">
759 class="btn btn-block btn-danger"
762 this.handleDeleteAccountShowConfirmToggle
765 {i18n.t('delete_account')}
767 {this.state.deleteAccountShowConfirm && (
769 <div class="my-2 alert alert-danger" role="alert">
770 {i18n.t('delete_account_confirm')}
774 value={this.state.deleteAccountForm.password}
775 autoComplete="new-password"
778 this.handleDeleteAccountPasswordChange
780 class="form-control my-2"
783 class="btn btn-danger mr-4"
784 disabled={!this.state.deleteAccountForm.password}
785 onClick={linkEvent(this, this.handleDeleteAccount)}
787 {this.state.deleteAccountLoading ? (
788 <svg class="icon icon-spinner spin">
789 <use xlinkHref="#icon-spinner"></use>
792 capitalizeFirstLetter(i18n.t('delete'))
796 class="btn btn-secondary"
799 this.handleDeleteAccountShowConfirmToggle
817 {this.state.moderates.length > 0 && (
818 <div class="card bg-transparent border-secondary mb-3">
819 <div class="card-body">
820 <h5>{i18n.t('moderates')}</h5>
821 <ul class="list-unstyled mb-0">
822 {this.state.moderates.map(community => (
824 <Link to={`/c/${community.community_name}`}>
825 {community.community_name}
840 {this.state.follows.length > 0 && (
841 <div class="card bg-transparent border-secondary mb-3">
842 <div class="card-body">
843 <h5>{i18n.t('subscribed')}</h5>
844 <ul class="list-unstyled mb-0">
845 {this.state.follows.map(community => (
847 <Link to={`/c/${community.community_name}`}>
848 {community.community_name}
860 updateUrl(paramUpdates: UrlParams) {
861 const page = paramUpdates.page || this.state.page;
863 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
865 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
866 this.props.history.push(
867 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
871 handlePageChange(page: number) {
872 this.updateUrl({ page });
875 handleSortChange(val: SortType) {
876 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
879 handleViewChange(i: User, event: any) {
881 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
886 handleUserSettingsShowNsfwChange(i: User, event: any) {
887 i.state.userSettingsForm.show_nsfw = event.target.checked;
891 handleUserSettingsShowAvatarsChange(i: User, event: any) {
892 i.state.userSettingsForm.show_avatars = event.target.checked;
893 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
897 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
898 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
902 handleUserSettingsThemeChange(i: User, event: any) {
903 i.state.userSettingsForm.theme = event.target.value;
904 setTheme(event.target.value, true);
908 handleUserSettingsLangChange(i: User, event: any) {
909 i.state.userSettingsForm.lang = event.target.value;
910 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
914 handleUserSettingsSortTypeChange(val: SortType) {
915 this.state.userSettingsForm.default_sort_type = val;
916 this.setState(this.state);
919 handleUserSettingsListingTypeChange(val: ListingType) {
920 this.state.userSettingsForm.default_listing_type = val;
921 this.setState(this.state);
924 handleUserSettingsEmailChange(i: User, event: any) {
925 i.state.userSettingsForm.email = event.target.value;
929 handleUserSettingsBioChange(val: string) {
930 this.state.userSettingsForm.bio = val;
931 this.setState(this.state);
934 handleAvatarUpload(url: string) {
935 this.state.userSettingsForm.avatar = url;
936 this.setState(this.state);
939 handleAvatarRemove() {
940 this.state.userSettingsForm.avatar = '';
941 this.setState(this.state);
944 handleBannerUpload(url: string) {
945 this.state.userSettingsForm.banner = url;
946 this.setState(this.state);
949 handleBannerRemove() {
950 this.state.userSettingsForm.banner = '';
951 this.setState(this.state);
954 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
955 i.state.userSettingsForm.preferred_username = event.target.value;
959 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
960 i.state.userSettingsForm.matrix_user_id = event.target.value;
962 i.state.userSettingsForm.matrix_user_id == '' &&
963 !i.state.user.matrix_user_id
965 i.state.userSettingsForm.matrix_user_id = undefined;
970 handleUserSettingsNewPasswordChange(i: User, event: any) {
971 i.state.userSettingsForm.new_password = event.target.value;
972 if (i.state.userSettingsForm.new_password == '') {
973 i.state.userSettingsForm.new_password = undefined;
978 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
979 i.state.userSettingsForm.new_password_verify = event.target.value;
980 if (i.state.userSettingsForm.new_password_verify == '') {
981 i.state.userSettingsForm.new_password_verify = undefined;
986 handleUserSettingsOldPasswordChange(i: User, event: any) {
987 i.state.userSettingsForm.old_password = event.target.value;
988 if (i.state.userSettingsForm.old_password == '') {
989 i.state.userSettingsForm.old_password = undefined;
994 handleUserSettingsSubmit(i: User, event: any) {
995 event.preventDefault();
996 i.state.userSettingsLoading = true;
999 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1002 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1003 event.preventDefault();
1004 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1005 i.setState(i.state);
1008 handleDeleteAccountPasswordChange(i: User, event: any) {
1009 i.state.deleteAccountForm.password = event.target.value;
1010 i.setState(i.state);
1013 handleLogoutClick(i: User) {
1014 UserService.Instance.logout();
1015 i.context.router.history.push('/');
1018 handleDeleteAccount(i: User, event: any) {
1019 event.preventDefault();
1020 i.state.deleteAccountLoading = true;
1021 i.setState(i.state);
1023 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1026 parseMessage(msg: WebSocketJsonResponse) {
1028 const res = wsJsonToRes(msg);
1030 toast(i18n.t(msg.error), 'danger');
1031 if (msg.error == 'couldnt_find_that_username_or_email') {
1032 this.context.router.history.push('/');
1035 deleteAccountLoading: false,
1036 userSettingsLoading: false,
1039 } else if (res.op == UserOperation.GetUserDetails) {
1040 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1041 // and set the parent state if it is not set or differs
1042 const data = res.data as UserDetailsResponse;
1044 if (this.state.user.id !== data.user.id) {
1045 this.state.user = data.user;
1046 this.state.follows = data.follows;
1047 this.state.moderates = data.moderates;
1049 if (this.isCurrentUser) {
1050 this.state.userSettingsForm.show_nsfw =
1051 UserService.Instance.user.show_nsfw;
1052 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1053 ? UserService.Instance.user.theme
1055 this.state.userSettingsForm.default_sort_type =
1056 UserService.Instance.user.default_sort_type;
1057 this.state.userSettingsForm.default_listing_type =
1058 UserService.Instance.user.default_listing_type;
1059 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1060 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1061 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1062 this.state.userSettingsForm.preferred_username =
1063 UserService.Instance.user.preferred_username;
1064 this.state.userSettingsForm.show_avatars =
1065 UserService.Instance.user.show_avatars;
1066 this.state.userSettingsForm.email = UserService.Instance.user.email;
1067 this.state.userSettingsForm.bio = UserService.Instance.user.bio;
1068 this.state.userSettingsForm.send_notifications_to_email =
1069 UserService.Instance.user.send_notifications_to_email;
1070 this.state.userSettingsForm.matrix_user_id =
1071 UserService.Instance.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);