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.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
158 this.handlePageChange = this.handlePageChange.bind(this);
159 this.handleUserSettingsBioChange = this.handleUserSettingsBioChange.bind(
163 this.state.user_id = Number(this.props.match.params.id) || null;
164 this.state.username = this.props.match.params.username;
166 this.subscription = WebSocketService.Instance.subject
167 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
169 msg => this.parseMessage(msg),
170 err => console.error(err),
171 () => console.log('complete')
174 WebSocketService.Instance.getSite();
177 get isCurrentUser() {
179 UserService.Instance.user &&
180 UserService.Instance.user.id == this.state.user.id
184 static getViewFromProps(view: any): UserDetailsView {
186 ? UserDetailsView[capitalizeFirstLetter(view)]
187 : UserDetailsView.Overview;
190 static getSortTypeFromProps(sort: any): SortType {
191 return sort ? routeSortTypeToEnum(sort) : SortType.New;
194 static getPageFromProps(page: any): number {
195 return page ? Number(page) : 1;
198 componentWillUnmount() {
199 this.subscription.unsubscribe();
202 static getDerivedStateFromProps(props: any): UserProps {
204 view: this.getViewFromProps(props.match.params.view),
205 sort: this.getSortTypeFromProps(props.match.params.sort),
206 page: this.getPageFromProps(props.match.params.page),
207 user_id: Number(props.match.params.id) || null,
208 username: props.match.params.username,
212 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
213 // Necessary if you are on a post and you click another post (same route)
215 lastProps.location.pathname.split('/')[2] !==
216 lastProps.history.location.pathname.split('/')[2]
218 // Couldnt get a refresh working. This does for now.
224 get documentTitle(): string {
225 if (this.state.siteRes.site.name) {
226 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
234 <div class="container">
235 <Helmet title={this.documentTitle} />
237 <div class="col-12 col-md-8">
239 {this.state.user.avatar && showAvatars() && (
243 src={this.state.user.avatar}
244 class="rounded-circle mr-2"
247 <span>@{this.state.username}</span>
249 {this.state.loading ? (
251 <svg class="icon icon-spinner spin">
252 <use xlinkHref="#icon-spinner"></use>
259 user_id={this.state.user_id}
260 username={this.state.username}
261 sort={SortType[this.state.sort]}
262 page={this.state.page}
264 enableDownvotes={this.state.siteRes.site.enable_downvotes}
265 enableNsfw={this.state.siteRes.site.enable_nsfw}
266 admins={this.state.siteRes.admins}
267 view={this.state.view}
268 onPageChange={this.handlePageChange}
272 {!this.state.loading && (
273 <div class="col-12 col-md-4">
275 {this.isCurrentUser && this.userSettings()}
287 <div class="btn-group btn-group-toggle flex-wrap mb-2">
289 className={`btn btn-outline-secondary pointer
290 ${this.state.view == UserDetailsView.Overview && 'active'}
295 value={UserDetailsView.Overview}
296 checked={this.state.view === UserDetailsView.Overview}
297 onChange={linkEvent(this, this.handleViewChange)}
302 className={`btn btn-outline-secondary pointer
303 ${this.state.view == UserDetailsView.Comments && 'active'}
308 value={UserDetailsView.Comments}
309 checked={this.state.view == UserDetailsView.Comments}
310 onChange={linkEvent(this, this.handleViewChange)}
315 className={`btn btn-outline-secondary pointer
316 ${this.state.view == UserDetailsView.Posts && 'active'}
321 value={UserDetailsView.Posts}
322 checked={this.state.view == UserDetailsView.Posts}
323 onChange={linkEvent(this, this.handleViewChange)}
328 className={`btn btn-outline-secondary pointer
329 ${this.state.view == UserDetailsView.Saved && 'active'}
334 value={UserDetailsView.Saved}
335 checked={this.state.view == UserDetailsView.Saved}
336 onChange={linkEvent(this, this.handleViewChange)}
346 <div className="mb-2">
347 <span class="mr-3">{this.viewRadios()}</span>
349 sort={this.state.sort}
350 onChange={this.handleSortChange}
354 href={`/feeds/u/${this.state.username}.xml?sort=${
355 SortType[this.state.sort]
361 <svg class="icon mx-2 text-muted small">
362 <use xlinkHref="#icon-rss">#</use>
370 let user = this.state.user;
373 <div class="card bg-transparent border-secondary mb-3">
374 <div class="card-body">
376 <ul class="list-inline mb-0">
377 <li className="list-inline-item">
378 <UserListing user={user} realLink />
381 <li className="list-inline-item badge badge-danger">
387 <div className="d-flex align-items-center mb-2">
390 dangerouslySetInnerHTML={mdToHtml(user.bio)}
393 <div className="d-flex align-items-center mb-2">
395 <use xlinkHref="#icon-cake"></use>
397 <span className="ml-2">
398 {i18n.t('cake_day_title')}{' '}
399 {moment.utc(user.published).local().format('MMM DD, YYYY')}
403 {i18n.t('joined')} <MomentTime data={user} showAgo />
405 <div class="table-responsive mt-1">
406 <table class="table table-bordered table-sm mt-2 mb-0">
409 <td class="text-center" colSpan={2}>
410 {i18n.t('number_of_points', {
411 count: user.post_score + user.comment_score,
419 {i18n.t('number_of_points', { count: user.post_score })}
423 {i18n.t('number_of_posts', { count: user.number_of_posts })}
429 {i18n.t('number_of_points', { count: user.comment_score })}
433 {i18n.t('number_of_comments', {
434 count: user.number_of_comments,
440 {this.isCurrentUser ? (
442 class="btn btn-block btn-secondary mt-3"
443 onClick={linkEvent(this, this.handleLogoutClick)}
450 className={`btn btn-block btn-secondary mt-3 ${
451 !this.state.user.matrix_user_id && 'disabled'
455 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
457 {i18n.t('send_secure_message')}
460 class="btn btn-block btn-secondary mt-3"
461 to={`/create_private_message?recipient_id=${this.state.user.id}`}
463 {i18n.t('send_message')}
476 <div class="card bg-transparent border-secondary mb-3">
477 <div class="card-body">
478 <h5>{i18n.t('settings')}</h5>
479 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
480 <div class="form-group">
481 <label>{i18n.t('avatar')}</label>
482 <form class="d-inline">
484 htmlFor="file-upload"
485 class="pointer ml-4 text-muted small font-weight-bold"
487 {!this.checkSettingsAvatar ? (
488 <span class="btn btn-secondary">
489 {i18n.t('upload_avatar')}
495 src={this.state.userSettingsForm.avatar}
496 class="rounded-circle"
503 accept="image/*,video/*"
506 disabled={!UserService.Instance.user}
507 onChange={linkEvent(this, this.handleImageUpload)}
511 {this.checkSettingsAvatar && (
512 <div class="form-group">
514 class="btn btn-secondary btn-block"
515 onClick={linkEvent(this, this.removeAvatar)}
517 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
523 <div class="form-group">
524 <label>{i18n.t('language')}</label>
526 value={this.state.userSettingsForm.lang}
527 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
528 class="ml-2 custom-select w-auto"
530 <option disabled>{i18n.t('language')}</option>
531 <option value="browser">{i18n.t('browser_default')}</option>
532 <option disabled>──</option>
533 {languages.map(lang => (
534 <option value={lang.code}>{lang.name}</option>
538 <div class="form-group">
539 <label>{i18n.t('theme')}</label>
541 value={this.state.userSettingsForm.theme}
542 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
543 class="ml-2 custom-select w-auto"
545 <option disabled>{i18n.t('theme')}</option>
546 {themes.map(theme => (
547 <option value={theme}>{theme}</option>
551 <form className="form-group">
553 <div class="mr-2">{i18n.t('sort_type')}</div>
556 type_={this.state.userSettingsForm.default_listing_type}
557 onChange={this.handleUserSettingsListingTypeChange}
560 <form className="form-group">
562 <div class="mr-2">{i18n.t('type')}</div>
565 sort={this.state.userSettingsForm.default_sort_type}
566 onChange={this.handleUserSettingsSortTypeChange}
569 <div class="form-group row">
570 <label class="col-lg-3 col-form-label" htmlFor="user-email">
573 <div class="col-lg-9">
578 placeholder={i18n.t('optional')}
579 value={this.state.userSettingsForm.email}
582 this.handleUserSettingsEmailChange
588 <div class="form-group row">
589 <label class="col-lg-3 col-form-label" htmlFor="user-bio">
592 <div class="col-lg-9">
594 initialContent={this.state.userSettingsForm.bio}
595 onContentChange={this.handleUserSettingsBioChange}
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);