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.user.avatar
242 ? this.state.user.avatar
243 : this.state.siteRes.site.icon
244 ? this.state.siteRes.site.icon
250 <div class="container">
251 <Helmet title={this.documentTitle}>
260 <div class="col-12 col-md-8">
261 {this.state.loading ? (
263 <svg class="icon icon-spinner spin">
264 <use xlinkHref="#icon-spinner"></use>
273 {!this.state.loading && this.selects()}
275 user_id={this.state.user_id}
276 username={this.state.username}
277 sort={SortType[this.state.sort]}
278 page={this.state.page}
280 enableDownvotes={this.state.siteRes.site.enable_downvotes}
281 enableNsfw={this.state.siteRes.site.enable_nsfw}
282 admins={this.state.siteRes.admins}
283 view={this.state.view}
284 onPageChange={this.handlePageChange}
288 {!this.state.loading && (
289 <div class="col-12 col-md-4">
290 {this.isCurrentUser && this.userSettings()}
302 <div class="btn-group btn-group-toggle flex-wrap mb-2">
304 className={`btn btn-outline-secondary pointer
305 ${this.state.view == UserDetailsView.Overview && 'active'}
310 value={UserDetailsView.Overview}
311 checked={this.state.view === UserDetailsView.Overview}
312 onChange={linkEvent(this, this.handleViewChange)}
317 className={`btn btn-outline-secondary pointer
318 ${this.state.view == UserDetailsView.Comments && 'active'}
323 value={UserDetailsView.Comments}
324 checked={this.state.view == UserDetailsView.Comments}
325 onChange={linkEvent(this, this.handleViewChange)}
330 className={`btn btn-outline-secondary pointer
331 ${this.state.view == UserDetailsView.Posts && 'active'}
336 value={UserDetailsView.Posts}
337 checked={this.state.view == UserDetailsView.Posts}
338 onChange={linkEvent(this, this.handleViewChange)}
343 className={`btn btn-outline-secondary pointer
344 ${this.state.view == UserDetailsView.Saved && 'active'}
349 value={UserDetailsView.Saved}
350 checked={this.state.view == UserDetailsView.Saved}
351 onChange={linkEvent(this, this.handleViewChange)}
361 <div className="mb-2">
362 <span class="mr-3">{this.viewRadios()}</span>
364 sort={this.state.sort}
365 onChange={this.handleSortChange}
369 href={`/feeds/u/${this.state.username}.xml?sort=${
370 SortType[this.state.sort]
376 <svg class="icon mx-2 text-muted small">
377 <use xlinkHref="#icon-rss">#</use>
385 let user = this.state.user;
390 banner={this.state.user.banner}
391 icon={this.state.user.avatar}
395 <div class="mb-0 d-flex flex-wrap">
397 {user.preferred_username && (
398 <h5 class="mb-0">{user.preferred_username}</h5>
400 <ul class="list-inline mb-2">
401 <li className="list-inline-item">
411 <li className="list-inline-item badge badge-danger">
417 <div className="flex-grow-1 unselectable pointer mx-2"></div>
418 {this.isCurrentUser ? (
420 class="d-flex align-self-start btn btn-secondary ml-2"
421 onClick={linkEvent(this, this.handleLogoutClick)}
428 className={`d-flex align-self-start btn btn-secondary ml-2 ${
429 !this.state.user.matrix_user_id && 'invisible'
433 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
435 {i18n.t('send_secure_message')}
438 class="d-flex align-self-start btn btn-secondary ml-2"
439 to={`/create_private_message?recipient_id=${this.state.user.id}`}
441 {i18n.t('send_message')}
447 <div className="d-flex align-items-center mb-2">
450 dangerouslySetInnerHTML={mdToHtml(user.bio)}
455 <ul class="list-inline mb-2">
456 <li className="list-inline-item badge badge-light">
457 {i18n.t('number_of_posts', { count: user.number_of_posts })}
459 <li className="list-inline-item badge badge-light">
460 {i18n.t('number_of_comments', {
461 count: user.number_of_comments,
466 <div class="text-muted">
467 {i18n.t('joined')} <MomentTime data={user} showAgo />
469 <div className="d-flex align-items-center text-muted mb-2">
471 <use xlinkHref="#icon-cake"></use>
473 <span className="ml-2">
474 {i18n.t('cake_day_title')}{' '}
475 {moment.utc(user.published).local().format('MMM DD, YYYY')}
487 <div class="card bg-transparent border-secondary mb-3">
488 <div class="card-body">
489 <h5>{i18n.t('settings')}</h5>
490 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
491 <div class="form-group">
492 <label>{i18n.t('avatar')}</label>
494 uploadTitle={i18n.t('upload_avatar')}
495 imageSrc={this.state.userSettingsForm.avatar}
496 onUpload={this.handleAvatarUpload}
497 onRemove={this.handleAvatarRemove}
501 <div class="form-group">
502 <label>{i18n.t('banner')}</label>
504 uploadTitle={i18n.t('upload_banner')}
505 imageSrc={this.state.userSettingsForm.banner}
506 onUpload={this.handleBannerUpload}
507 onRemove={this.handleBannerRemove}
510 <div class="form-group">
511 <label>{i18n.t('language')}</label>
513 value={this.state.userSettingsForm.lang}
514 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
515 class="ml-2 custom-select w-auto"
517 <option disabled>{i18n.t('language')}</option>
518 <option value="browser">{i18n.t('browser_default')}</option>
519 <option disabled>──</option>
520 {languages.map(lang => (
521 <option value={lang.code}>{lang.name}</option>
525 <div class="form-group">
526 <label>{i18n.t('theme')}</label>
528 value={this.state.userSettingsForm.theme}
529 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
530 class="ml-2 custom-select w-auto"
532 <option disabled>{i18n.t('theme')}</option>
533 {themes.map(theme => (
534 <option value={theme}>{theme}</option>
538 <form className="form-group">
540 <div class="mr-2">{i18n.t('sort_type')}</div>
543 type_={this.state.userSettingsForm.default_listing_type}
544 onChange={this.handleUserSettingsListingTypeChange}
547 <form className="form-group">
549 <div class="mr-2">{i18n.t('type')}</div>
552 sort={this.state.userSettingsForm.default_sort_type}
553 onChange={this.handleUserSettingsSortTypeChange}
556 <div class="form-group row">
557 <label class="col-lg-5 col-form-label">
558 {i18n.t('display_name')}
560 <div class="col-lg-7">
564 placeholder={i18n.t('optional')}
565 value={this.state.userSettingsForm.preferred_username}
568 this.handleUserSettingsPreferredUsernameChange
575 <div class="form-group row">
576 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
579 <div class="col-lg-9">
581 initialContent={this.state.userSettingsForm.bio}
582 onContentChange={this.handleUserSettingsBioChange}
584 hideNavigationWarnings
588 <div class="form-group row">
589 <label class="col-lg-3 col-form-label" htmlFor="user-email">
592 <div class="col-lg-9">
597 placeholder={i18n.t('optional')}
598 value={this.state.userSettingsForm.email}
601 this.handleUserSettingsEmailChange
607 <div class="form-group row">
608 <label class="col-lg-5 col-form-label">
609 <a href={elementUrl} target="_blank" rel="noopener">
610 {i18n.t('matrix_user_id')}
613 <div class="col-lg-7">
617 placeholder="@user:example.com"
618 value={this.state.userSettingsForm.matrix_user_id}
621 this.handleUserSettingsMatrixUserIdChange
627 <div class="form-group row">
628 <label class="col-lg-5 col-form-label" htmlFor="user-password">
629 {i18n.t('new_password')}
631 <div class="col-lg-7">
636 value={this.state.userSettingsForm.new_password}
637 autoComplete="new-password"
640 this.handleUserSettingsNewPasswordChange
645 <div class="form-group row">
647 class="col-lg-5 col-form-label"
648 htmlFor="user-verify-password"
650 {i18n.t('verify_password')}
652 <div class="col-lg-7">
655 id="user-verify-password"
657 value={this.state.userSettingsForm.new_password_verify}
658 autoComplete="new-password"
661 this.handleUserSettingsNewPasswordVerifyChange
666 <div class="form-group row">
668 class="col-lg-5 col-form-label"
669 htmlFor="user-old-password"
671 {i18n.t('old_password')}
673 <div class="col-lg-7">
676 id="user-old-password"
678 value={this.state.userSettingsForm.old_password}
679 autoComplete="new-password"
682 this.handleUserSettingsOldPasswordChange
687 {this.state.siteRes.site.enable_nsfw && (
688 <div class="form-group">
689 <div class="form-check">
691 class="form-check-input"
694 checked={this.state.userSettingsForm.show_nsfw}
697 this.handleUserSettingsShowNsfwChange
700 <label class="form-check-label" htmlFor="user-show-nsfw">
701 {i18n.t('show_nsfw')}
706 <div class="form-group">
707 <div class="form-check">
709 class="form-check-input"
710 id="user-show-avatars"
712 checked={this.state.userSettingsForm.show_avatars}
715 this.handleUserSettingsShowAvatarsChange
718 <label class="form-check-label" htmlFor="user-show-avatars">
719 {i18n.t('show_avatars')}
723 <div class="form-group">
724 <div class="form-check">
726 class="form-check-input"
727 id="user-send-notifications-to-email"
729 disabled={!this.state.user.email}
731 this.state.userSettingsForm.send_notifications_to_email
735 this.handleUserSettingsSendNotificationsToEmailChange
739 class="form-check-label"
740 htmlFor="user-send-notifications-to-email"
742 {i18n.t('send_notifications_to_email')}
746 <div class="form-group">
747 <button type="submit" class="btn btn-block btn-secondary mr-4">
748 {this.state.userSettingsLoading ? (
749 <svg class="icon icon-spinner spin">
750 <use xlinkHref="#icon-spinner"></use>
753 capitalizeFirstLetter(i18n.t('save'))
758 <div class="form-group mb-0">
760 class="btn btn-block btn-danger"
763 this.handleDeleteAccountShowConfirmToggle
766 {i18n.t('delete_account')}
768 {this.state.deleteAccountShowConfirm && (
770 <div class="my-2 alert alert-danger" role="alert">
771 {i18n.t('delete_account_confirm')}
775 value={this.state.deleteAccountForm.password}
776 autoComplete="new-password"
779 this.handleDeleteAccountPasswordChange
781 class="form-control my-2"
784 class="btn btn-danger mr-4"
785 disabled={!this.state.deleteAccountForm.password}
786 onClick={linkEvent(this, this.handleDeleteAccount)}
788 {this.state.deleteAccountLoading ? (
789 <svg class="icon icon-spinner spin">
790 <use xlinkHref="#icon-spinner"></use>
793 capitalizeFirstLetter(i18n.t('delete'))
797 class="btn btn-secondary"
800 this.handleDeleteAccountShowConfirmToggle
818 {this.state.moderates.length > 0 && (
819 <div class="card bg-transparent border-secondary mb-3">
820 <div class="card-body">
821 <h5>{i18n.t('moderates')}</h5>
822 <ul class="list-unstyled mb-0">
823 {this.state.moderates.map(community => (
825 <Link to={`/c/${community.community_name}`}>
826 {community.community_name}
841 {this.state.follows.length > 0 && (
842 <div class="card bg-transparent border-secondary mb-3">
843 <div class="card-body">
844 <h5>{i18n.t('subscribed')}</h5>
845 <ul class="list-unstyled mb-0">
846 {this.state.follows.map(community => (
848 <Link to={`/c/${community.community_name}`}>
849 {community.community_name}
861 updateUrl(paramUpdates: UrlParams) {
862 const page = paramUpdates.page || this.state.page;
864 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
866 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
867 this.props.history.push(
868 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
872 handlePageChange(page: number) {
873 this.updateUrl({ page });
876 handleSortChange(val: SortType) {
877 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
880 handleViewChange(i: User, event: any) {
882 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
887 handleUserSettingsShowNsfwChange(i: User, event: any) {
888 i.state.userSettingsForm.show_nsfw = event.target.checked;
892 handleUserSettingsShowAvatarsChange(i: User, event: any) {
893 i.state.userSettingsForm.show_avatars = event.target.checked;
894 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
898 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
899 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
903 handleUserSettingsThemeChange(i: User, event: any) {
904 i.state.userSettingsForm.theme = event.target.value;
905 setTheme(event.target.value, true);
909 handleUserSettingsLangChange(i: User, event: any) {
910 i.state.userSettingsForm.lang = event.target.value;
911 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
915 handleUserSettingsSortTypeChange(val: SortType) {
916 this.state.userSettingsForm.default_sort_type = val;
917 this.setState(this.state);
920 handleUserSettingsListingTypeChange(val: ListingType) {
921 this.state.userSettingsForm.default_listing_type = val;
922 this.setState(this.state);
925 handleUserSettingsEmailChange(i: User, event: any) {
926 i.state.userSettingsForm.email = event.target.value;
927 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
928 i.state.userSettingsForm.email = undefined;
933 handleUserSettingsBioChange(val: string) {
934 this.state.userSettingsForm.bio = val;
935 this.setState(this.state);
938 handleAvatarUpload(url: string) {
939 this.state.userSettingsForm.avatar = url;
940 this.setState(this.state);
943 handleAvatarRemove() {
944 this.state.userSettingsForm.avatar = '';
945 this.setState(this.state);
948 handleBannerUpload(url: string) {
949 this.state.userSettingsForm.banner = url;
950 this.setState(this.state);
953 handleBannerRemove() {
954 this.state.userSettingsForm.banner = '';
955 this.setState(this.state);
958 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
959 i.state.userSettingsForm.preferred_username = event.target.value;
963 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
964 i.state.userSettingsForm.matrix_user_id = event.target.value;
966 i.state.userSettingsForm.matrix_user_id == '' &&
967 !i.state.user.matrix_user_id
969 i.state.userSettingsForm.matrix_user_id = undefined;
974 handleUserSettingsNewPasswordChange(i: User, event: any) {
975 i.state.userSettingsForm.new_password = event.target.value;
976 if (i.state.userSettingsForm.new_password == '') {
977 i.state.userSettingsForm.new_password = undefined;
982 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
983 i.state.userSettingsForm.new_password_verify = event.target.value;
984 if (i.state.userSettingsForm.new_password_verify == '') {
985 i.state.userSettingsForm.new_password_verify = undefined;
990 handleUserSettingsOldPasswordChange(i: User, event: any) {
991 i.state.userSettingsForm.old_password = event.target.value;
992 if (i.state.userSettingsForm.old_password == '') {
993 i.state.userSettingsForm.old_password = undefined;
998 handleUserSettingsSubmit(i: User, event: any) {
999 event.preventDefault();
1000 i.state.userSettingsLoading = true;
1001 i.setState(i.state);
1003 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1006 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1007 event.preventDefault();
1008 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1009 i.setState(i.state);
1012 handleDeleteAccountPasswordChange(i: User, event: any) {
1013 i.state.deleteAccountForm.password = event.target.value;
1014 i.setState(i.state);
1017 handleLogoutClick(i: User) {
1018 UserService.Instance.logout();
1019 i.context.router.history.push('/');
1022 handleDeleteAccount(i: User, event: any) {
1023 event.preventDefault();
1024 i.state.deleteAccountLoading = true;
1025 i.setState(i.state);
1027 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1030 parseMessage(msg: WebSocketJsonResponse) {
1032 const res = wsJsonToRes(msg);
1034 toast(i18n.t(msg.error), 'danger');
1035 if (msg.error == 'couldnt_find_that_username_or_email') {
1036 this.context.router.history.push('/');
1039 deleteAccountLoading: false,
1040 userSettingsLoading: false,
1043 } else if (res.op == UserOperation.GetUserDetails) {
1044 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1045 // and set the parent state if it is not set or differs
1046 const data = res.data as UserDetailsResponse;
1048 if (this.state.user.id !== data.user.id) {
1049 this.state.user = data.user;
1050 this.state.follows = data.follows;
1051 this.state.moderates = data.moderates;
1053 if (this.isCurrentUser) {
1054 this.state.userSettingsForm.show_nsfw =
1055 UserService.Instance.user.show_nsfw;
1056 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1057 ? UserService.Instance.user.theme
1059 this.state.userSettingsForm.default_sort_type =
1060 UserService.Instance.user.default_sort_type;
1061 this.state.userSettingsForm.default_listing_type =
1062 UserService.Instance.user.default_listing_type;
1063 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1064 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1065 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1066 this.state.userSettingsForm.preferred_username =
1067 UserService.Instance.user.preferred_username;
1068 this.state.userSettingsForm.email = this.state.user.email;
1069 this.state.userSettingsForm.bio = this.state.user.bio;
1070 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1071 this.state.userSettingsForm.show_avatars =
1072 UserService.Instance.user.show_avatars;
1073 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1075 this.state.loading = false;
1076 this.setState(this.state);
1078 } else if (res.op == UserOperation.SaveUserSettings) {
1079 const data = res.data as LoginResponse;
1080 UserService.Instance.login(data);
1081 this.state.user.bio = this.state.userSettingsForm.bio;
1082 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1083 this.state.user.banner = this.state.userSettingsForm.banner;
1084 this.state.user.avatar = this.state.userSettingsForm.avatar;
1085 this.state.userSettingsLoading = false;
1086 this.setState(this.state);
1088 window.scrollTo(0, 0);
1089 } else if (res.op == UserOperation.DeleteAccount) {
1091 deleteAccountLoading: false,
1092 deleteAccountShowConfirm: false,
1094 this.context.router.history.push('/');
1095 } else if (res.op == UserOperation.GetSite) {
1096 const data = res.data as GetSiteResponse;
1097 this.state.siteRes = data;
1098 this.setState(this.state);
1099 } else if (res.op == UserOperation.AddAdmin) {
1100 const data = res.data as AddAdminResponse;
1101 this.state.siteRes.admins = data.admins;
1102 this.setState(this.state);