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,
36 import { UserListing } from './user-listing';
37 import { SortSelect } from './sort-select';
38 import { ListingTypeSelect } from './listing-type-select';
39 import { MomentTime } from './moment-time';
40 import { i18n } from '../i18next';
41 import moment from 'moment';
42 import { UserDetails } from './user-details';
43 import { MarkdownTextArea } from './markdown-textarea';
49 follows: Array<CommunityUser>;
50 moderates: Array<CommunityUser>;
51 view: UserDetailsView;
55 avatarLoading: boolean;
56 userSettingsForm: UserSettingsForm;
57 userSettingsLoading: boolean;
58 deleteAccountLoading: boolean;
59 deleteAccountShowConfirm: boolean;
60 deleteAccountForm: DeleteAccountForm;
61 siteRes: GetSiteResponse;
65 view: UserDetailsView;
68 user_id: number | null;
78 export class User extends Component<any, UserState> {
79 private subscription: Subscription;
80 private emptyState: UserState = {
85 number_of_posts: null,
87 number_of_comments: null,
92 send_notifications_to_email: null,
101 avatarLoading: false,
102 view: User.getViewFromProps(this.props.match.view),
103 sort: User.getSortTypeFromProps(this.props.match.sort),
104 page: User.getPageFromProps(this.props.match.page),
108 default_sort_type: null,
109 default_listing_type: null,
112 send_notifications_to_email: 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,
144 constructor(props: any, context: any) {
145 super(props, context);
147 this.state = this.emptyState;
148 this.handleSortChange = this.handleSortChange.bind(this);
149 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
152 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
155 this.handlePageChange = this.handlePageChange.bind(this);
156 this.handleUserSettingsBioChange = this.handleUserSettingsBioChange.bind(
160 this.state.user_id = Number(this.props.match.params.id) || null;
161 this.state.username = this.props.match.params.username;
163 this.subscription = WebSocketService.Instance.subject
164 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
166 msg => this.parseMessage(msg),
167 err => console.error(err),
168 () => console.log('complete')
171 WebSocketService.Instance.getSite();
174 get isCurrentUser() {
176 UserService.Instance.user &&
177 UserService.Instance.user.id == this.state.user.id
181 static getViewFromProps(view: any): UserDetailsView {
183 ? UserDetailsView[capitalizeFirstLetter(view)]
184 : UserDetailsView.Overview;
187 static getSortTypeFromProps(sort: any): SortType {
188 return sort ? routeSortTypeToEnum(sort) : SortType.New;
191 static getPageFromProps(page: any): number {
192 return page ? Number(page) : 1;
195 componentWillUnmount() {
196 this.subscription.unsubscribe();
199 static getDerivedStateFromProps(props: any): UserProps {
201 view: this.getViewFromProps(props.match.params.view),
202 sort: this.getSortTypeFromProps(props.match.params.sort),
203 page: this.getPageFromProps(props.match.params.page),
204 user_id: Number(props.match.params.id) || null,
205 username: props.match.params.username,
209 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
210 // Necessary if you are on a post and you click another post (same route)
212 lastProps.location.pathname.split('/')[2] !==
213 lastProps.history.location.pathname.split('/')[2]
215 // Couldnt get a refresh working. This does for now.
221 get documentTitle(): string {
222 if (this.state.siteRes.site.name) {
223 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
231 <div class="container">
232 <Helmet title={this.documentTitle} />
234 <div class="col-12 col-md-8">
236 {this.state.user.avatar && showAvatars() && (
240 src={this.state.user.avatar}
241 class="rounded-circle mr-2"
244 <span>@{this.state.username}</span>
246 {this.state.loading ? (
248 <svg class="icon icon-spinner spin">
249 <use xlinkHref="#icon-spinner"></use>
256 user_id={this.state.user_id}
257 username={this.state.username}
258 sort={SortType[this.state.sort]}
259 page={this.state.page}
261 enableDownvotes={this.state.siteRes.site.enable_downvotes}
262 enableNsfw={this.state.siteRes.site.enable_nsfw}
263 admins={this.state.siteRes.admins}
264 view={this.state.view}
265 onPageChange={this.handlePageChange}
269 {!this.state.loading && (
270 <div class="col-12 col-md-4">
272 {this.isCurrentUser && this.userSettings()}
284 <div class="btn-group btn-group-toggle flex-wrap mb-2">
286 className={`btn btn-outline-secondary pointer
287 ${this.state.view == UserDetailsView.Overview && 'active'}
292 value={UserDetailsView.Overview}
293 checked={this.state.view === UserDetailsView.Overview}
294 onChange={linkEvent(this, this.handleViewChange)}
299 className={`btn btn-outline-secondary pointer
300 ${this.state.view == UserDetailsView.Comments && 'active'}
305 value={UserDetailsView.Comments}
306 checked={this.state.view == UserDetailsView.Comments}
307 onChange={linkEvent(this, this.handleViewChange)}
312 className={`btn btn-outline-secondary pointer
313 ${this.state.view == UserDetailsView.Posts && 'active'}
318 value={UserDetailsView.Posts}
319 checked={this.state.view == UserDetailsView.Posts}
320 onChange={linkEvent(this, this.handleViewChange)}
325 className={`btn btn-outline-secondary pointer
326 ${this.state.view == UserDetailsView.Saved && 'active'}
331 value={UserDetailsView.Saved}
332 checked={this.state.view == UserDetailsView.Saved}
333 onChange={linkEvent(this, this.handleViewChange)}
343 <div className="mb-2">
344 <span class="mr-3">{this.viewRadios()}</span>
346 sort={this.state.sort}
347 onChange={this.handleSortChange}
351 href={`/feeds/u/${this.state.username}.xml?sort=${
352 SortType[this.state.sort]
358 <svg class="icon mx-2 text-muted small">
359 <use xlinkHref="#icon-rss">#</use>
367 let user = this.state.user;
370 <div class="card bg-transparent border-secondary mb-3">
371 <div class="card-body">
373 <ul class="list-inline mb-0">
374 <li className="list-inline-item">
375 <UserListing user={user} realLink />
378 <li className="list-inline-item badge badge-danger">
385 <div className="d-flex align-items-center mb-2">
388 dangerouslySetInnerHTML={mdToHtml(user.bio)}
392 <div className="d-flex align-items-center mb-2">
394 <use xlinkHref="#icon-cake"></use>
396 <span className="ml-2">
397 {i18n.t('cake_day_title')}{' '}
398 {moment.utc(user.published).local().format('MMM DD, YYYY')}
402 {i18n.t('joined')} <MomentTime data={user} showAgo />
404 <div class="table-responsive mt-1">
405 <table class="table table-bordered table-sm mt-2 mb-0">
408 <td class="text-center" colSpan={2}>
409 {i18n.t('number_of_points', {
410 count: user.post_score + user.comment_score,
418 {i18n.t('number_of_points', { count: user.post_score })}
422 {i18n.t('number_of_posts', { count: user.number_of_posts })}
428 {i18n.t('number_of_points', { count: user.comment_score })}
432 {i18n.t('number_of_comments', {
433 count: user.number_of_comments,
439 {this.isCurrentUser ? (
441 class="btn btn-block btn-secondary mt-3"
442 onClick={linkEvent(this, this.handleLogoutClick)}
449 className={`btn btn-block btn-secondary mt-3 ${
450 !this.state.user.matrix_user_id && 'disabled'
454 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
456 {i18n.t('send_secure_message')}
459 class="btn btn-block btn-secondary mt-3"
460 to={`/create_private_message?recipient_id=${this.state.user.id}`}
462 {i18n.t('send_message')}
475 <div class="card bg-transparent border-secondary mb-3">
476 <div class="card-body">
477 <h5>{i18n.t('settings')}</h5>
478 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
479 <div class="form-group">
480 <label>{i18n.t('avatar')}</label>
481 <form class="d-inline">
483 htmlFor="file-upload"
484 class="pointer ml-4 text-muted small font-weight-bold"
486 {!this.checkSettingsAvatar ? (
487 <span class="btn btn-secondary">
488 {i18n.t('upload_avatar')}
494 src={this.state.userSettingsForm.avatar}
495 class="rounded-circle"
502 accept="image/*,video/*"
505 disabled={!UserService.Instance.user}
506 onChange={linkEvent(this, this.handleImageUpload)}
510 {this.checkSettingsAvatar && (
511 <div class="form-group">
513 class="btn btn-secondary btn-block"
514 onClick={linkEvent(this, this.removeAvatar)}
516 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
522 <div class="form-group">
523 <label>{i18n.t('language')}</label>
525 value={this.state.userSettingsForm.lang}
526 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
527 class="ml-2 custom-select w-auto"
529 <option disabled>{i18n.t('language')}</option>
530 <option value="browser">{i18n.t('browser_default')}</option>
531 <option disabled>──</option>
532 {languages.map(lang => (
533 <option value={lang.code}>{lang.name}</option>
537 <div class="form-group">
538 <label>{i18n.t('theme')}</label>
540 value={this.state.userSettingsForm.theme}
541 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
542 class="ml-2 custom-select w-auto"
544 <option disabled>{i18n.t('theme')}</option>
545 {themes.map(theme => (
546 <option value={theme}>{theme}</option>
550 <form className="form-group">
552 <div class="mr-2">{i18n.t('sort_type')}</div>
555 type_={this.state.userSettingsForm.default_listing_type}
556 onChange={this.handleUserSettingsListingTypeChange}
559 <form className="form-group">
561 <div class="mr-2">{i18n.t('type')}</div>
564 sort={this.state.userSettingsForm.default_sort_type}
565 onChange={this.handleUserSettingsSortTypeChange}
568 <div class="form-group row">
569 <label class="col-lg-3 col-form-label" htmlFor="user-email">
572 <div class="col-lg-9">
577 placeholder={i18n.t('optional')}
578 value={this.state.userSettingsForm.email}
581 this.handleUserSettingsEmailChange
587 <div class="form-group row">
588 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
591 <div class="col-lg-9">
593 initialContent={this.state.userSettingsForm.bio}
594 onContentChange={this.handleUserSettingsBioChange}
596 hideNavigationWarnings
600 <div class="form-group row">
601 <label class="col-lg-5 col-form-label">
603 href="https://about.riot.im/"
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.user.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;
924 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
925 i.state.userSettingsForm.email = undefined;
930 handleUserSettingsBioChange(val: string) {
931 this.state.userSettingsForm.bio = val;
932 this.setState(this.state);
935 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
936 i.state.userSettingsForm.matrix_user_id = event.target.value;
938 i.state.userSettingsForm.matrix_user_id == '' &&
939 !i.state.user.matrix_user_id
941 i.state.userSettingsForm.matrix_user_id = undefined;
946 handleUserSettingsNewPasswordChange(i: User, event: any) {
947 i.state.userSettingsForm.new_password = event.target.value;
948 if (i.state.userSettingsForm.new_password == '') {
949 i.state.userSettingsForm.new_password = undefined;
954 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
955 i.state.userSettingsForm.new_password_verify = event.target.value;
956 if (i.state.userSettingsForm.new_password_verify == '') {
957 i.state.userSettingsForm.new_password_verify = undefined;
962 handleUserSettingsOldPasswordChange(i: User, event: any) {
963 i.state.userSettingsForm.old_password = event.target.value;
964 if (i.state.userSettingsForm.old_password == '') {
965 i.state.userSettingsForm.old_password = undefined;
970 handleImageUpload(i: User, event: any) {
971 event.preventDefault();
972 let file = event.target.files[0];
973 const imageUploadUrl = `/pictrs/image`;
974 const formData = new FormData();
975 formData.append('images[]', file);
977 i.state.avatarLoading = true;
980 fetch(imageUploadUrl, {
984 .then(res => res.json())
986 console.log('pictrs upload:');
988 if (res.msg == 'ok') {
989 let hash = res.files[0].file;
990 let url = `${window.location.origin}/pictrs/image/${hash}`;
991 i.state.userSettingsForm.avatar = url;
992 i.state.avatarLoading = false;
995 i.state.avatarLoading = false;
997 toast(JSON.stringify(res), 'danger');
1001 i.state.avatarLoading = false;
1002 i.setState(i.state);
1003 toast(error, 'danger');
1007 removeAvatar(i: User, event: any) {
1008 event.preventDefault();
1009 i.state.userSettingsLoading = true;
1010 i.state.userSettingsForm.avatar = '';
1011 i.setState(i.state);
1013 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1016 get checkSettingsAvatar(): boolean {
1018 this.state.userSettingsForm.avatar &&
1019 this.state.userSettingsForm.avatar != ''
1023 handleUserSettingsSubmit(i: User, event: any) {
1024 event.preventDefault();
1025 i.state.userSettingsLoading = true;
1026 i.setState(i.state);
1028 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1031 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1032 event.preventDefault();
1033 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1034 i.setState(i.state);
1037 handleDeleteAccountPasswordChange(i: User, event: any) {
1038 i.state.deleteAccountForm.password = event.target.value;
1039 i.setState(i.state);
1042 handleLogoutClick(i: User) {
1043 UserService.Instance.logout();
1044 i.context.router.history.push('/');
1047 handleDeleteAccount(i: User, event: any) {
1048 event.preventDefault();
1049 i.state.deleteAccountLoading = true;
1050 i.setState(i.state);
1052 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1055 parseMessage(msg: WebSocketJsonResponse) {
1057 const res = wsJsonToRes(msg);
1059 toast(i18n.t(msg.error), 'danger');
1060 if (msg.error == 'couldnt_find_that_username_or_email') {
1061 this.context.router.history.push('/');
1064 deleteAccountLoading: false,
1065 avatarLoading: false,
1066 userSettingsLoading: false,
1069 } else if (res.op == UserOperation.GetUserDetails) {
1070 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1071 // and set the parent state if it is not set or differs
1072 const data = res.data as UserDetailsResponse;
1074 if (this.state.user.id !== data.user.id) {
1075 this.state.user = data.user;
1076 this.state.follows = data.follows;
1077 this.state.moderates = data.moderates;
1079 if (this.isCurrentUser) {
1080 this.state.userSettingsForm.show_nsfw =
1081 UserService.Instance.user.show_nsfw;
1082 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1083 ? UserService.Instance.user.theme
1085 this.state.userSettingsForm.default_sort_type =
1086 UserService.Instance.user.default_sort_type;
1087 this.state.userSettingsForm.default_listing_type =
1088 UserService.Instance.user.default_listing_type;
1089 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1090 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1091 this.state.userSettingsForm.email = this.state.user.email;
1092 this.state.userSettingsForm.bio = this.state.user.bio;
1093 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1094 this.state.userSettingsForm.show_avatars =
1095 UserService.Instance.user.show_avatars;
1096 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1098 this.state.loading = false;
1099 this.setState(this.state);
1101 } else if (res.op == UserOperation.SaveUserSettings) {
1102 const data = res.data as LoginResponse;
1103 UserService.Instance.login(data);
1104 this.state.user.bio = this.state.userSettingsForm.bio;
1105 this.state.userSettingsLoading = false;
1106 this.setState(this.state);
1108 window.scrollTo(0, 0);
1109 } else if (res.op == UserOperation.DeleteAccount) {
1111 deleteAccountLoading: false,
1112 deleteAccountShowConfirm: false,
1114 this.context.router.history.push('/');
1115 } else if (res.op == UserOperation.GetSite) {
1116 const data = res.data as GetSiteResponse;
1117 this.state.siteRes = data;
1118 this.setState(this.state);
1119 } else if (res.op == UserOperation.AddAdmin) {
1120 const data = res.data as AddAdminResponse;
1121 this.state.siteRes.admins = data.admins;
1122 this.setState(this.state);