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,
148 constructor(props: any, context: any) {
149 super(props, context);
151 this.state = this.emptyState;
152 this.handleSortChange = this.handleSortChange.bind(this);
153 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
156 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
159 this.handlePageChange = this.handlePageChange.bind(this);
160 this.handleUserSettingsBioChange = this.handleUserSettingsBioChange.bind(
164 this.handleAvatarUpload = this.handleAvatarUpload.bind(this);
165 this.handleAvatarRemove = this.handleAvatarRemove.bind(this);
167 this.handleBannerUpload = this.handleBannerUpload.bind(this);
168 this.handleBannerRemove = this.handleBannerRemove.bind(this);
170 this.state.user_id = Number(this.props.match.params.id) || null;
171 this.state.username = this.props.match.params.username;
173 this.subscription = WebSocketService.Instance.subject
174 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
176 msg => this.parseMessage(msg),
177 err => console.error(err),
178 () => console.log('complete')
181 WebSocketService.Instance.getSite();
184 get isCurrentUser() {
186 UserService.Instance.user &&
187 UserService.Instance.user.id == this.state.user.id
191 static getViewFromProps(view: any): UserDetailsView {
193 ? UserDetailsView[capitalizeFirstLetter(view)]
194 : UserDetailsView.Overview;
197 static getSortTypeFromProps(sort: any): SortType {
198 return sort ? routeSortTypeToEnum(sort) : SortType.New;
201 static getPageFromProps(page: any): 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.
231 get documentTitle(): string {
232 if (this.state.siteRes.site.name) {
233 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
239 get favIcon(): string {
240 return this.state.siteRes.site.icon
241 ? this.state.siteRes.site.icon
247 <div class="container">
248 <Helmet title={this.documentTitle}>
257 <div class="col-12 col-md-8">
258 {this.state.loading ? (
260 <svg class="icon icon-spinner spin">
261 <use xlinkHref="#icon-spinner"></use>
270 {!this.state.loading && this.selects()}
272 user_id={this.state.user_id}
273 username={this.state.username}
274 sort={SortType[this.state.sort]}
275 page={this.state.page}
277 enableDownvotes={this.state.siteRes.site.enable_downvotes}
278 enableNsfw={this.state.siteRes.site.enable_nsfw}
279 admins={this.state.siteRes.admins}
280 view={this.state.view}
281 onPageChange={this.handlePageChange}
285 {!this.state.loading && (
286 <div class="col-12 col-md-4">
287 {this.isCurrentUser && this.userSettings()}
299 <div class="btn-group btn-group-toggle flex-wrap mb-2">
301 className={`btn btn-outline-secondary pointer
302 ${this.state.view == UserDetailsView.Overview && 'active'}
307 value={UserDetailsView.Overview}
308 checked={this.state.view === UserDetailsView.Overview}
309 onChange={linkEvent(this, this.handleViewChange)}
314 className={`btn btn-outline-secondary pointer
315 ${this.state.view == UserDetailsView.Comments && 'active'}
320 value={UserDetailsView.Comments}
321 checked={this.state.view == UserDetailsView.Comments}
322 onChange={linkEvent(this, this.handleViewChange)}
327 className={`btn btn-outline-secondary pointer
328 ${this.state.view == UserDetailsView.Posts && 'active'}
333 value={UserDetailsView.Posts}
334 checked={this.state.view == UserDetailsView.Posts}
335 onChange={linkEvent(this, this.handleViewChange)}
340 className={`btn btn-outline-secondary pointer
341 ${this.state.view == UserDetailsView.Saved && 'active'}
346 value={UserDetailsView.Saved}
347 checked={this.state.view == UserDetailsView.Saved}
348 onChange={linkEvent(this, this.handleViewChange)}
358 <div className="mb-2">
359 <span class="mr-3">{this.viewRadios()}</span>
361 sort={this.state.sort}
362 onChange={this.handleSortChange}
366 href={`/feeds/u/${this.state.username}.xml?sort=${
367 SortType[this.state.sort]
373 <svg class="icon mx-2 text-muted small">
374 <use xlinkHref="#icon-rss">#</use>
382 let user = this.state.user;
387 banner={this.state.user.banner}
388 icon={this.state.user.avatar}
392 <div class="mb-0 d-flex flex-wrap">
394 {user.preferred_username && (
395 <h5 class="mb-0">{user.preferred_username}</h5>
397 <ul class="list-inline mb-2">
398 <li className="list-inline-item">
408 <li className="list-inline-item badge badge-danger">
414 <div className="flex-grow-1 unselectable pointer mx-2"></div>
415 {this.isCurrentUser ? (
417 class="d-flex align-self-start btn btn-secondary ml-2"
418 onClick={linkEvent(this, this.handleLogoutClick)}
425 className={`d-flex align-self-start btn btn-secondary ml-2 ${
426 !this.state.user.matrix_user_id && 'invisible'
430 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
432 {i18n.t('send_secure_message')}
435 class="d-flex align-self-start btn btn-secondary ml-2"
436 to={`/create_private_message?recipient_id=${this.state.user.id}`}
438 {i18n.t('send_message')}
444 <div className="d-flex align-items-center mb-2">
447 dangerouslySetInnerHTML={mdToHtml(user.bio)}
452 <ul class="list-inline mb-2">
453 <li className="list-inline-item badge badge-light">
454 {i18n.t('number_of_posts', { count: user.number_of_posts })}
456 <li className="list-inline-item badge badge-light">
457 {i18n.t('number_of_comments', {
458 count: user.number_of_comments,
463 <div class="text-muted">
464 {i18n.t('joined')} <MomentTime data={user} showAgo />
466 <div className="d-flex align-items-center text-muted mb-2">
468 <use xlinkHref="#icon-cake"></use>
470 <span className="ml-2">
471 {i18n.t('cake_day_title')}{' '}
472 {moment.utc(user.published).local().format('MMM DD, YYYY')}
484 <div class="card bg-transparent border-secondary mb-3">
485 <div class="card-body">
486 <h5>{i18n.t('settings')}</h5>
487 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
488 <div class="form-group">
489 <label>{i18n.t('avatar')}</label>
491 uploadTitle={i18n.t('upload_avatar')}
492 imageSrc={this.state.userSettingsForm.avatar}
493 onUpload={this.handleAvatarUpload}
494 onRemove={this.handleAvatarRemove}
498 <div class="form-group">
499 <label>{i18n.t('banner')}</label>
501 uploadTitle={i18n.t('upload_banner')}
502 imageSrc={this.state.userSettingsForm.banner}
503 onUpload={this.handleBannerUpload}
504 onRemove={this.handleBannerRemove}
507 <div class="form-group">
508 <label>{i18n.t('language')}</label>
510 value={this.state.userSettingsForm.lang}
511 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
512 class="ml-2 custom-select w-auto"
514 <option disabled>{i18n.t('language')}</option>
515 <option value="browser">{i18n.t('browser_default')}</option>
516 <option disabled>──</option>
517 {languages.map(lang => (
518 <option value={lang.code}>{lang.name}</option>
522 <div class="form-group">
523 <label>{i18n.t('theme')}</label>
525 value={this.state.userSettingsForm.theme}
526 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
527 class="ml-2 custom-select w-auto"
529 <option disabled>{i18n.t('theme')}</option>
530 {themes.map(theme => (
531 <option value={theme}>{theme}</option>
535 <form className="form-group">
537 <div class="mr-2">{i18n.t('sort_type')}</div>
540 type_={this.state.userSettingsForm.default_listing_type}
541 onChange={this.handleUserSettingsListingTypeChange}
544 <form className="form-group">
546 <div class="mr-2">{i18n.t('type')}</div>
549 sort={this.state.userSettingsForm.default_sort_type}
550 onChange={this.handleUserSettingsSortTypeChange}
553 <div class="form-group row">
554 <label class="col-lg-5 col-form-label">
555 {i18n.t('display_name')}
557 <div class="col-lg-7">
561 placeholder={i18n.t('optional')}
562 value={this.state.userSettingsForm.preferred_username}
565 this.handleUserSettingsPreferredUsernameChange
572 <div class="form-group row">
573 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
576 <div class="col-lg-9">
578 initialContent={this.state.userSettingsForm.bio}
579 onContentChange={this.handleUserSettingsBioChange}
581 hideNavigationWarnings
585 <div class="form-group row">
586 <label class="col-lg-3 col-form-label" htmlFor="user-email">
589 <div class="col-lg-9">
594 placeholder={i18n.t('optional')}
595 value={this.state.userSettingsForm.email}
598 this.handleUserSettingsEmailChange
604 <div class="form-group row">
605 <label class="col-lg-5 col-form-label">
606 <a href={elementUrl} target="_blank" rel="noopener">
607 {i18n.t('matrix_user_id')}
610 <div class="col-lg-7">
614 placeholder="@user:example.com"
615 value={this.state.userSettingsForm.matrix_user_id}
618 this.handleUserSettingsMatrixUserIdChange
624 <div class="form-group row">
625 <label class="col-lg-5 col-form-label" htmlFor="user-password">
626 {i18n.t('new_password')}
628 <div class="col-lg-7">
633 value={this.state.userSettingsForm.new_password}
634 autoComplete="new-password"
637 this.handleUserSettingsNewPasswordChange
642 <div class="form-group row">
644 class="col-lg-5 col-form-label"
645 htmlFor="user-verify-password"
647 {i18n.t('verify_password')}
649 <div class="col-lg-7">
652 id="user-verify-password"
654 value={this.state.userSettingsForm.new_password_verify}
655 autoComplete="new-password"
658 this.handleUserSettingsNewPasswordVerifyChange
663 <div class="form-group row">
665 class="col-lg-5 col-form-label"
666 htmlFor="user-old-password"
668 {i18n.t('old_password')}
670 <div class="col-lg-7">
673 id="user-old-password"
675 value={this.state.userSettingsForm.old_password}
676 autoComplete="new-password"
679 this.handleUserSettingsOldPasswordChange
684 {this.state.siteRes.site.enable_nsfw && (
685 <div class="form-group">
686 <div class="form-check">
688 class="form-check-input"
691 checked={this.state.userSettingsForm.show_nsfw}
694 this.handleUserSettingsShowNsfwChange
697 <label class="form-check-label" htmlFor="user-show-nsfw">
698 {i18n.t('show_nsfw')}
703 <div class="form-group">
704 <div class="form-check">
706 class="form-check-input"
707 id="user-show-avatars"
709 checked={this.state.userSettingsForm.show_avatars}
712 this.handleUserSettingsShowAvatarsChange
715 <label class="form-check-label" htmlFor="user-show-avatars">
716 {i18n.t('show_avatars')}
720 <div class="form-group">
721 <div class="form-check">
723 class="form-check-input"
724 id="user-send-notifications-to-email"
726 disabled={!this.state.userSettingsForm.email}
728 this.state.userSettingsForm.send_notifications_to_email
732 this.handleUserSettingsSendNotificationsToEmailChange
736 class="form-check-label"
737 htmlFor="user-send-notifications-to-email"
739 {i18n.t('send_notifications_to_email')}
743 <div class="form-group">
744 <button type="submit" class="btn btn-block btn-secondary mr-4">
745 {this.state.userSettingsLoading ? (
746 <svg class="icon icon-spinner spin">
747 <use xlinkHref="#icon-spinner"></use>
750 capitalizeFirstLetter(i18n.t('save'))
755 <div class="form-group mb-0">
757 class="btn btn-block btn-danger"
760 this.handleDeleteAccountShowConfirmToggle
763 {i18n.t('delete_account')}
765 {this.state.deleteAccountShowConfirm && (
767 <div class="my-2 alert alert-danger" role="alert">
768 {i18n.t('delete_account_confirm')}
772 value={this.state.deleteAccountForm.password}
773 autoComplete="new-password"
776 this.handleDeleteAccountPasswordChange
778 class="form-control my-2"
781 class="btn btn-danger mr-4"
782 disabled={!this.state.deleteAccountForm.password}
783 onClick={linkEvent(this, this.handleDeleteAccount)}
785 {this.state.deleteAccountLoading ? (
786 <svg class="icon icon-spinner spin">
787 <use xlinkHref="#icon-spinner"></use>
790 capitalizeFirstLetter(i18n.t('delete'))
794 class="btn btn-secondary"
797 this.handleDeleteAccountShowConfirmToggle
815 {this.state.moderates.length > 0 && (
816 <div class="card bg-transparent border-secondary mb-3">
817 <div class="card-body">
818 <h5>{i18n.t('moderates')}</h5>
819 <ul class="list-unstyled mb-0">
820 {this.state.moderates.map(community => (
822 <Link to={`/c/${community.community_name}`}>
823 {community.community_name}
838 {this.state.follows.length > 0 && (
839 <div class="card bg-transparent border-secondary mb-3">
840 <div class="card-body">
841 <h5>{i18n.t('subscribed')}</h5>
842 <ul class="list-unstyled mb-0">
843 {this.state.follows.map(community => (
845 <Link to={`/c/${community.community_name}`}>
846 {community.community_name}
858 updateUrl(paramUpdates: UrlParams) {
859 const page = paramUpdates.page || this.state.page;
861 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
863 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
864 this.props.history.push(
865 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
869 handlePageChange(page: number) {
870 this.updateUrl({ page });
873 handleSortChange(val: SortType) {
874 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
877 handleViewChange(i: User, event: any) {
879 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
884 handleUserSettingsShowNsfwChange(i: User, event: any) {
885 i.state.userSettingsForm.show_nsfw = event.target.checked;
889 handleUserSettingsShowAvatarsChange(i: User, event: any) {
890 i.state.userSettingsForm.show_avatars = event.target.checked;
891 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
895 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
896 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
900 handleUserSettingsThemeChange(i: User, event: any) {
901 i.state.userSettingsForm.theme = event.target.value;
902 setTheme(event.target.value, true);
906 handleUserSettingsLangChange(i: User, event: any) {
907 i.state.userSettingsForm.lang = event.target.value;
908 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
912 handleUserSettingsSortTypeChange(val: SortType) {
913 this.state.userSettingsForm.default_sort_type = val;
914 this.setState(this.state);
917 handleUserSettingsListingTypeChange(val: ListingType) {
918 this.state.userSettingsForm.default_listing_type = val;
919 this.setState(this.state);
922 handleUserSettingsEmailChange(i: User, event: any) {
923 i.state.userSettingsForm.email = event.target.value;
927 handleUserSettingsBioChange(val: string) {
928 this.state.userSettingsForm.bio = val;
929 this.setState(this.state);
932 handleAvatarUpload(url: string) {
933 this.state.userSettingsForm.avatar = url;
934 this.setState(this.state);
937 handleAvatarRemove() {
938 this.state.userSettingsForm.avatar = '';
939 this.setState(this.state);
942 handleBannerUpload(url: string) {
943 this.state.userSettingsForm.banner = url;
944 this.setState(this.state);
947 handleBannerRemove() {
948 this.state.userSettingsForm.banner = '';
949 this.setState(this.state);
952 handleUserSettingsPreferredUsernameChange(i: User, event: any) {
953 i.state.userSettingsForm.preferred_username = event.target.value;
957 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
958 i.state.userSettingsForm.matrix_user_id = event.target.value;
960 i.state.userSettingsForm.matrix_user_id == '' &&
961 !i.state.user.matrix_user_id
963 i.state.userSettingsForm.matrix_user_id = undefined;
968 handleUserSettingsNewPasswordChange(i: User, event: any) {
969 i.state.userSettingsForm.new_password = event.target.value;
970 if (i.state.userSettingsForm.new_password == '') {
971 i.state.userSettingsForm.new_password = undefined;
976 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
977 i.state.userSettingsForm.new_password_verify = event.target.value;
978 if (i.state.userSettingsForm.new_password_verify == '') {
979 i.state.userSettingsForm.new_password_verify = undefined;
984 handleUserSettingsOldPasswordChange(i: User, event: any) {
985 i.state.userSettingsForm.old_password = event.target.value;
986 if (i.state.userSettingsForm.old_password == '') {
987 i.state.userSettingsForm.old_password = undefined;
992 handleUserSettingsSubmit(i: User, event: any) {
993 event.preventDefault();
994 i.state.userSettingsLoading = true;
997 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1000 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1001 event.preventDefault();
1002 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1003 i.setState(i.state);
1006 handleDeleteAccountPasswordChange(i: User, event: any) {
1007 i.state.deleteAccountForm.password = event.target.value;
1008 i.setState(i.state);
1011 handleLogoutClick(i: User) {
1012 UserService.Instance.logout();
1013 i.context.router.history.push('/');
1016 handleDeleteAccount(i: User, event: any) {
1017 event.preventDefault();
1018 i.state.deleteAccountLoading = true;
1019 i.setState(i.state);
1021 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1024 parseMessage(msg: WebSocketJsonResponse) {
1026 const res = wsJsonToRes(msg);
1028 toast(i18n.t(msg.error), 'danger');
1029 if (msg.error == 'couldnt_find_that_username_or_email') {
1030 this.context.router.history.push('/');
1033 deleteAccountLoading: false,
1034 userSettingsLoading: false,
1037 } else if (res.op == UserOperation.GetUserDetails) {
1038 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1039 // and set the parent state if it is not set or differs
1040 const data = res.data as UserDetailsResponse;
1042 if (this.state.user.id !== data.user.id) {
1043 this.state.user = data.user;
1044 this.state.follows = data.follows;
1045 this.state.moderates = data.moderates;
1047 if (this.isCurrentUser) {
1048 this.state.userSettingsForm.show_nsfw =
1049 UserService.Instance.user.show_nsfw;
1050 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1051 ? UserService.Instance.user.theme
1053 this.state.userSettingsForm.default_sort_type =
1054 UserService.Instance.user.default_sort_type;
1055 this.state.userSettingsForm.default_listing_type =
1056 UserService.Instance.user.default_listing_type;
1057 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1058 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1059 this.state.userSettingsForm.banner = UserService.Instance.user.banner;
1060 this.state.userSettingsForm.preferred_username =
1061 UserService.Instance.user.preferred_username;
1062 this.state.userSettingsForm.show_avatars =
1063 UserService.Instance.user.show_avatars;
1064 this.state.userSettingsForm.email = UserService.Instance.user.email;
1065 this.state.userSettingsForm.bio = UserService.Instance.user.bio;
1066 this.state.userSettingsForm.send_notifications_to_email =
1067 UserService.Instance.user.send_notifications_to_email;
1068 this.state.userSettingsForm.matrix_user_id =
1069 UserService.Instance.user.matrix_user_id;
1071 this.state.loading = false;
1072 this.setState(this.state);
1074 } else if (res.op == UserOperation.SaveUserSettings) {
1075 const data = res.data as LoginResponse;
1076 UserService.Instance.login(data);
1077 this.state.user.bio = this.state.userSettingsForm.bio;
1078 this.state.user.preferred_username = this.state.userSettingsForm.preferred_username;
1079 this.state.user.banner = this.state.userSettingsForm.banner;
1080 this.state.user.avatar = this.state.userSettingsForm.avatar;
1081 this.state.userSettingsLoading = false;
1082 this.setState(this.state);
1084 window.scrollTo(0, 0);
1085 } else if (res.op == UserOperation.DeleteAccount) {
1087 deleteAccountLoading: false,
1088 deleteAccountShowConfirm: false,
1090 this.context.router.history.push('/');
1091 } else if (res.op == UserOperation.GetSite) {
1092 const data = res.data as GetSiteResponse;
1093 this.state.siteRes = data;
1094 this.setState(this.state);
1095 } else if (res.op == UserOperation.AddAdmin) {
1096 const data = res.data as AddAdminResponse;
1097 this.state.siteRes.admins = data.admins;
1098 this.setState(this.state);