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}
599 <div class="form-group row">
600 <label class="col-lg-5 col-form-label">
602 href="https://about.riot.im/"
606 {i18n.t('matrix_user_id')}
609 <div class="col-lg-7">
613 placeholder="@user:example.com"
614 value={this.state.userSettingsForm.matrix_user_id}
617 this.handleUserSettingsMatrixUserIdChange
623 <div class="form-group row">
624 <label class="col-lg-5 col-form-label" htmlFor="user-password">
625 {i18n.t('new_password')}
627 <div class="col-lg-7">
632 value={this.state.userSettingsForm.new_password}
633 autoComplete="new-password"
636 this.handleUserSettingsNewPasswordChange
641 <div class="form-group row">
643 class="col-lg-5 col-form-label"
644 htmlFor="user-verify-password"
646 {i18n.t('verify_password')}
648 <div class="col-lg-7">
651 id="user-verify-password"
653 value={this.state.userSettingsForm.new_password_verify}
654 autoComplete="new-password"
657 this.handleUserSettingsNewPasswordVerifyChange
662 <div class="form-group row">
664 class="col-lg-5 col-form-label"
665 htmlFor="user-old-password"
667 {i18n.t('old_password')}
669 <div class="col-lg-7">
672 id="user-old-password"
674 value={this.state.userSettingsForm.old_password}
675 autoComplete="new-password"
678 this.handleUserSettingsOldPasswordChange
683 {this.state.siteRes.site.enable_nsfw && (
684 <div class="form-group">
685 <div class="form-check">
687 class="form-check-input"
690 checked={this.state.userSettingsForm.show_nsfw}
693 this.handleUserSettingsShowNsfwChange
696 <label class="form-check-label" htmlFor="user-show-nsfw">
697 {i18n.t('show_nsfw')}
702 <div class="form-group">
703 <div class="form-check">
705 class="form-check-input"
706 id="user-show-avatars"
708 checked={this.state.userSettingsForm.show_avatars}
711 this.handleUserSettingsShowAvatarsChange
714 <label class="form-check-label" htmlFor="user-show-avatars">
715 {i18n.t('show_avatars')}
719 <div class="form-group">
720 <div class="form-check">
722 class="form-check-input"
723 id="user-send-notifications-to-email"
725 disabled={!this.state.user.email}
727 this.state.userSettingsForm.send_notifications_to_email
731 this.handleUserSettingsSendNotificationsToEmailChange
735 class="form-check-label"
736 htmlFor="user-send-notifications-to-email"
738 {i18n.t('send_notifications_to_email')}
742 <div class="form-group">
743 <button type="submit" class="btn btn-block btn-secondary mr-4">
744 {this.state.userSettingsLoading ? (
745 <svg class="icon icon-spinner spin">
746 <use xlinkHref="#icon-spinner"></use>
749 capitalizeFirstLetter(i18n.t('save'))
754 <div class="form-group mb-0">
756 class="btn btn-block btn-danger"
759 this.handleDeleteAccountShowConfirmToggle
762 {i18n.t('delete_account')}
764 {this.state.deleteAccountShowConfirm && (
766 <div class="my-2 alert alert-danger" role="alert">
767 {i18n.t('delete_account_confirm')}
771 value={this.state.deleteAccountForm.password}
772 autoComplete="new-password"
775 this.handleDeleteAccountPasswordChange
777 class="form-control my-2"
780 class="btn btn-danger mr-4"
781 disabled={!this.state.deleteAccountForm.password}
782 onClick={linkEvent(this, this.handleDeleteAccount)}
784 {this.state.deleteAccountLoading ? (
785 <svg class="icon icon-spinner spin">
786 <use xlinkHref="#icon-spinner"></use>
789 capitalizeFirstLetter(i18n.t('delete'))
793 class="btn btn-secondary"
796 this.handleDeleteAccountShowConfirmToggle
814 {this.state.moderates.length > 0 && (
815 <div class="card bg-transparent border-secondary mb-3">
816 <div class="card-body">
817 <h5>{i18n.t('moderates')}</h5>
818 <ul class="list-unstyled mb-0">
819 {this.state.moderates.map(community => (
821 <Link to={`/c/${community.community_name}`}>
822 {community.community_name}
837 {this.state.follows.length > 0 && (
838 <div class="card bg-transparent border-secondary mb-3">
839 <div class="card-body">
840 <h5>{i18n.t('subscribed')}</h5>
841 <ul class="list-unstyled mb-0">
842 {this.state.follows.map(community => (
844 <Link to={`/c/${community.community_name}`}>
845 {community.community_name}
857 updateUrl(paramUpdates: UrlParams) {
858 const page = paramUpdates.page || this.state.page;
860 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
862 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
863 this.props.history.push(
864 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
868 handlePageChange(page: number) {
869 this.updateUrl({ page });
872 handleSortChange(val: SortType) {
873 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
876 handleViewChange(i: User, event: any) {
878 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
883 handleUserSettingsShowNsfwChange(i: User, event: any) {
884 i.state.userSettingsForm.show_nsfw = event.target.checked;
888 handleUserSettingsShowAvatarsChange(i: User, event: any) {
889 i.state.userSettingsForm.show_avatars = event.target.checked;
890 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
894 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
895 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
899 handleUserSettingsThemeChange(i: User, event: any) {
900 i.state.userSettingsForm.theme = event.target.value;
901 setTheme(event.target.value, true);
905 handleUserSettingsLangChange(i: User, event: any) {
906 i.state.userSettingsForm.lang = event.target.value;
907 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
911 handleUserSettingsSortTypeChange(val: SortType) {
912 this.state.userSettingsForm.default_sort_type = val;
913 this.setState(this.state);
916 handleUserSettingsListingTypeChange(val: ListingType) {
917 this.state.userSettingsForm.default_listing_type = val;
918 this.setState(this.state);
921 handleUserSettingsEmailChange(i: User, event: any) {
922 i.state.userSettingsForm.email = event.target.value;
923 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
924 i.state.userSettingsForm.email = undefined;
929 handleUserSettingsBioChange(val: string) {
930 this.state.userSettingsForm.bio = val;
931 this.setState(this.state);
934 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
935 i.state.userSettingsForm.matrix_user_id = event.target.value;
937 i.state.userSettingsForm.matrix_user_id == '' &&
938 !i.state.user.matrix_user_id
940 i.state.userSettingsForm.matrix_user_id = undefined;
945 handleUserSettingsNewPasswordChange(i: User, event: any) {
946 i.state.userSettingsForm.new_password = event.target.value;
947 if (i.state.userSettingsForm.new_password == '') {
948 i.state.userSettingsForm.new_password = undefined;
953 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
954 i.state.userSettingsForm.new_password_verify = event.target.value;
955 if (i.state.userSettingsForm.new_password_verify == '') {
956 i.state.userSettingsForm.new_password_verify = undefined;
961 handleUserSettingsOldPasswordChange(i: User, event: any) {
962 i.state.userSettingsForm.old_password = event.target.value;
963 if (i.state.userSettingsForm.old_password == '') {
964 i.state.userSettingsForm.old_password = undefined;
969 handleImageUpload(i: User, event: any) {
970 event.preventDefault();
971 let file = event.target.files[0];
972 const imageUploadUrl = `/pictrs/image`;
973 const formData = new FormData();
974 formData.append('images[]', file);
976 i.state.avatarLoading = true;
979 fetch(imageUploadUrl, {
983 .then(res => res.json())
985 console.log('pictrs upload:');
987 if (res.msg == 'ok') {
988 let hash = res.files[0].file;
989 let url = `${window.location.origin}/pictrs/image/${hash}`;
990 i.state.userSettingsForm.avatar = url;
991 i.state.avatarLoading = false;
994 i.state.avatarLoading = false;
996 toast(JSON.stringify(res), 'danger');
1000 i.state.avatarLoading = false;
1001 i.setState(i.state);
1002 toast(error, 'danger');
1006 removeAvatar(i: User, event: any) {
1007 event.preventDefault();
1008 i.state.userSettingsLoading = true;
1009 i.state.userSettingsForm.avatar = '';
1010 i.setState(i.state);
1012 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1015 get checkSettingsAvatar(): boolean {
1017 this.state.userSettingsForm.avatar &&
1018 this.state.userSettingsForm.avatar != ''
1022 handleUserSettingsSubmit(i: User, event: any) {
1023 event.preventDefault();
1024 i.state.userSettingsLoading = true;
1025 i.setState(i.state);
1027 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1030 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1031 event.preventDefault();
1032 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1033 i.setState(i.state);
1036 handleDeleteAccountPasswordChange(i: User, event: any) {
1037 i.state.deleteAccountForm.password = event.target.value;
1038 i.setState(i.state);
1041 handleLogoutClick(i: User) {
1042 UserService.Instance.logout();
1043 i.context.router.history.push('/');
1046 handleDeleteAccount(i: User, event: any) {
1047 event.preventDefault();
1048 i.state.deleteAccountLoading = true;
1049 i.setState(i.state);
1051 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1054 parseMessage(msg: WebSocketJsonResponse) {
1056 const res = wsJsonToRes(msg);
1058 toast(i18n.t(msg.error), 'danger');
1059 if (msg.error == 'couldnt_find_that_username_or_email') {
1060 this.context.router.history.push('/');
1063 deleteAccountLoading: false,
1064 avatarLoading: false,
1065 userSettingsLoading: false,
1068 } else if (res.op == UserOperation.GetUserDetails) {
1069 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1070 // and set the parent state if it is not set or differs
1071 const data = res.data as UserDetailsResponse;
1073 if (this.state.user.id !== data.user.id) {
1074 this.state.user = data.user;
1075 this.state.follows = data.follows;
1076 this.state.moderates = data.moderates;
1078 if (this.isCurrentUser) {
1079 this.state.userSettingsForm.show_nsfw =
1080 UserService.Instance.user.show_nsfw;
1081 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1082 ? UserService.Instance.user.theme
1084 this.state.userSettingsForm.default_sort_type =
1085 UserService.Instance.user.default_sort_type;
1086 this.state.userSettingsForm.default_listing_type =
1087 UserService.Instance.user.default_listing_type;
1088 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1089 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1090 this.state.userSettingsForm.email = this.state.user.email;
1091 this.state.userSettingsForm.bio = this.state.user.bio;
1092 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1093 this.state.userSettingsForm.show_avatars =
1094 UserService.Instance.user.show_avatars;
1095 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1097 this.state.loading = false;
1098 this.setState(this.state);
1100 } else if (res.op == UserOperation.SaveUserSettings) {
1101 const data = res.data as LoginResponse;
1102 UserService.Instance.login(data);
1103 this.state.user.bio = this.state.userSettingsForm.bio;
1104 this.state.userSettingsLoading = false;
1105 this.setState(this.state);
1107 window.scrollTo(0, 0);
1108 } else if (res.op == UserOperation.DeleteAccount) {
1110 deleteAccountLoading: false,
1111 deleteAccountShowConfirm: false,
1113 this.context.router.history.push('/');
1114 } else if (res.op == UserOperation.GetSite) {
1115 const data = res.data as GetSiteResponse;
1116 this.state.siteRes = data;
1117 this.setState(this.state);
1118 } else if (res.op == UserOperation.AddAdmin) {
1119 const data = res.data as AddAdminResponse;
1120 this.state.siteRes.admins = data.admins;
1121 this.setState(this.state);