1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
14 WebSocketJsonResponse,
19 } from '../interfaces';
20 import { WebSocketService, UserService } from '../services';
25 capitalizeFirstLetter,
33 import { UserListing } from './user-listing';
34 import { SortSelect } from './sort-select';
35 import { ListingTypeSelect } from './listing-type-select';
36 import { MomentTime } from './moment-time';
37 import { i18n } from '../i18next';
38 import moment from 'moment';
39 import { UserDetails } from './user-details';
45 follows: Array<CommunityUser>;
46 moderates: Array<CommunityUser>;
47 view: UserDetailsView;
51 avatarLoading: boolean;
52 userSettingsForm: UserSettingsForm;
53 userSettingsLoading: boolean;
54 deleteAccountLoading: boolean;
55 deleteAccountShowConfirm: boolean;
56 deleteAccountForm: DeleteAccountForm;
57 siteRes: GetSiteResponse;
61 view: UserDetailsView;
64 user_id: number | null;
74 export class User extends Component<any, UserState> {
75 private subscription: Subscription;
76 private emptyState: UserState = {
81 number_of_posts: null,
83 number_of_comments: null,
88 send_notifications_to_email: null,
98 view: User.getViewFromProps(this.props.match.view),
99 sort: User.getSortTypeFromProps(this.props.match.sort),
100 page: User.getPageFromProps(this.props.match.page),
104 default_sort_type: null,
105 default_listing_type: null,
108 send_notifications_to_email: null,
111 userSettingsLoading: null,
112 deleteAccountLoading: null,
113 deleteAccountShowConfirm: false,
124 creator_id: undefined,
125 published: undefined,
126 creator_name: undefined,
127 number_of_users: undefined,
128 number_of_posts: undefined,
129 number_of_comments: undefined,
130 number_of_communities: undefined,
131 enable_downvotes: undefined,
132 open_registration: undefined,
133 enable_nsfw: undefined,
138 constructor(props: any, context: any) {
139 super(props, context);
141 this.state = this.emptyState;
142 this.handleSortChange = this.handleSortChange.bind(this);
143 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
146 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
149 this.handlePageChange = this.handlePageChange.bind(this);
151 this.state.user_id = Number(this.props.match.params.id) || null;
152 this.state.username = this.props.match.params.username;
154 this.subscription = WebSocketService.Instance.subject
155 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
157 msg => this.parseMessage(msg),
158 err => console.error(err),
159 () => console.log('complete')
162 WebSocketService.Instance.getSite();
165 get isCurrentUser() {
167 UserService.Instance.user &&
168 UserService.Instance.user.id == this.state.user.id
172 static getViewFromProps(view: any): UserDetailsView {
174 ? UserDetailsView[capitalizeFirstLetter(view)]
175 : UserDetailsView.Overview;
178 static getSortTypeFromProps(sort: any): SortType {
179 return sort ? routeSortTypeToEnum(sort) : SortType.New;
182 static getPageFromProps(page: any): number {
183 return page ? Number(page) : 1;
186 componentWillUnmount() {
187 this.subscription.unsubscribe();
190 static getDerivedStateFromProps(props: any): UserProps {
192 view: this.getViewFromProps(props.match.params.view),
193 sort: this.getSortTypeFromProps(props.match.params.sort),
194 page: this.getPageFromProps(props.match.params.page),
195 user_id: Number(props.match.params.id) || null,
196 username: props.match.params.username,
200 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
201 // Necessary if you are on a post and you click another post (same route)
203 lastProps.location.pathname.split('/')[2] !==
204 lastProps.history.location.pathname.split('/')[2]
206 // Couldnt get a refresh working. This does for now.
209 document.title = `/u/${this.state.username} - ${this.state.siteRes.site.name}`;
215 <div class="container">
217 <div class="col-12 col-md-8">
219 {this.state.user.avatar && showAvatars() && (
223 src={this.state.user.avatar}
224 class="rounded-circle mr-2"
227 <span>/u/{this.state.username}</span>
229 {this.state.loading ? (
231 <svg class="icon icon-spinner spin">
232 <use xlinkHref="#icon-spinner"></use>
239 user_id={this.state.user_id}
240 username={this.state.username}
241 sort={SortType[this.state.sort]}
242 page={this.state.page}
244 enableDownvotes={this.state.siteRes.site.enable_downvotes}
245 enableNsfw={this.state.siteRes.site.enable_nsfw}
246 admins={this.state.siteRes.admins}
247 view={this.state.view}
248 onPageChange={this.handlePageChange}
252 {!this.state.loading && (
253 <div class="col-12 col-md-4">
255 {this.isCurrentUser && this.userSettings()}
267 <div class="btn-group btn-group-toggle">
269 className={`btn btn-sm btn-secondary pointer btn-outline-light
270 ${this.state.view == UserDetailsView.Overview && 'active'}
275 value={UserDetailsView.Overview}
276 checked={this.state.view === UserDetailsView.Overview}
277 onChange={linkEvent(this, this.handleViewChange)}
282 className={`btn btn-sm btn-secondary pointer btn-outline-light
283 ${this.state.view == UserDetailsView.Comments && 'active'}
288 value={UserDetailsView.Comments}
289 checked={this.state.view == UserDetailsView.Comments}
290 onChange={linkEvent(this, this.handleViewChange)}
295 className={`btn btn-sm btn-secondary pointer btn-outline-light
296 ${this.state.view == UserDetailsView.Posts && 'active'}
301 value={UserDetailsView.Posts}
302 checked={this.state.view == UserDetailsView.Posts}
303 onChange={linkEvent(this, this.handleViewChange)}
308 className={`btn btn-sm btn-secondary pointer btn-outline-light
309 ${this.state.view == UserDetailsView.Saved && 'active'}
314 value={UserDetailsView.Saved}
315 checked={this.state.view == UserDetailsView.Saved}
316 onChange={linkEvent(this, this.handleViewChange)}
326 <div className="mb-2">
327 <span class="mr-3">{this.viewRadios()}</span>
329 sort={this.state.sort}
330 onChange={this.handleSortChange}
334 href={`/feeds/u/${this.state.username}.xml?sort=${
335 SortType[this.state.sort]
341 <svg class="icon mx-2 text-muted small">
342 <use xlinkHref="#icon-rss">#</use>
350 let user = this.state.user;
353 <div class="card border-secondary mb-3">
354 <div class="card-body">
356 <ul class="list-inline mb-0">
357 <li className="list-inline-item">
358 <UserListing user={user} realLink />
361 <li className="list-inline-item badge badge-danger">
367 <div className="d-flex align-items-center mb-2">
369 <use xlinkHref="#icon-cake"></use>
371 <span className="ml-2">
372 {i18n.t('cake_day_title')}{' '}
373 {moment.utc(user.published).local().format('MMM DD, YYYY')}
377 {i18n.t('joined')} <MomentTime data={user} showAgo />
379 <div class="table-responsive mt-1">
380 <table class="table table-bordered table-sm mt-2 mb-0">
383 <td class="text-center" colSpan={2}>
384 {i18n.t('number_of_points', {
385 count: user.post_score + user.comment_score,
393 {i18n.t('number_of_points', { count: user.post_score })}
397 {i18n.t('number_of_posts', { count: user.number_of_posts })}
403 {i18n.t('number_of_points', { count: user.comment_score })}
407 {i18n.t('number_of_comments', {
408 count: user.number_of_comments,
414 {this.isCurrentUser ? (
416 class="btn btn-block btn-secondary mt-3"
417 onClick={linkEvent(this, this.handleLogoutClick)}
424 className={`btn btn-block btn-secondary mt-3 ${
425 !this.state.user.matrix_user_id && 'disabled'
429 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
431 {i18n.t('send_secure_message')}
434 class="btn btn-block btn-secondary mt-3"
435 to={`/create_private_message?recipient_id=${this.state.user.id}`}
437 {i18n.t('send_message')}
450 <div class="card border-secondary mb-3">
451 <div class="card-body">
452 <h5>{i18n.t('settings')}</h5>
453 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
454 <div class="form-group">
455 <label>{i18n.t('avatar')}</label>
456 <form class="d-inline">
458 htmlFor="file-upload"
459 class="pointer ml-4 text-muted small font-weight-bold"
461 {!this.checkSettingsAvatar ? (
462 <span class="btn btn-sm btn-secondary">
463 {i18n.t('upload_avatar')}
469 src={this.state.userSettingsForm.avatar}
470 class="rounded-circle"
477 accept="image/*,video/*"
480 disabled={!UserService.Instance.user}
481 onChange={linkEvent(this, this.handleImageUpload)}
485 {this.checkSettingsAvatar && (
486 <div class="form-group">
488 class="btn btn-secondary btn-block"
489 onClick={linkEvent(this, this.removeAvatar)}
491 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
497 <div class="form-group">
498 <label>{i18n.t('language')}</label>
500 value={this.state.userSettingsForm.lang}
501 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
502 class="ml-2 custom-select custom-select-sm w-auto"
504 <option disabled>{i18n.t('language')}</option>
505 <option value="browser">{i18n.t('browser_default')}</option>
506 <option disabled>──</option>
507 {languages.map(lang => (
508 <option value={lang.code}>{lang.name}</option>
512 <div class="form-group">
513 <label>{i18n.t('theme')}</label>
515 value={this.state.userSettingsForm.theme}
516 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
517 class="ml-2 custom-select custom-select-sm w-auto"
519 <option disabled>{i18n.t('theme')}</option>
520 {themes.map(theme => (
521 <option value={theme}>{theme}</option>
525 <form className="form-group">
527 <div class="mr-2">{i18n.t('sort_type')}</div>
530 type_={this.state.userSettingsForm.default_listing_type}
531 onChange={this.handleUserSettingsListingTypeChange}
534 <form className="form-group">
536 <div class="mr-2">{i18n.t('type')}</div>
539 sort={this.state.userSettingsForm.default_sort_type}
540 onChange={this.handleUserSettingsSortTypeChange}
543 <div class="form-group row">
544 <label class="col-lg-3 col-form-label" htmlFor="user-email">
547 <div class="col-lg-9">
552 placeholder={i18n.t('optional')}
553 value={this.state.userSettingsForm.email}
556 this.handleUserSettingsEmailChange
562 <div class="form-group row">
563 <label class="col-lg-5 col-form-label">
565 href="https://about.riot.im/"
569 {i18n.t('matrix_user_id')}
572 <div class="col-lg-7">
576 placeholder="@user:example.com"
577 value={this.state.userSettingsForm.matrix_user_id}
580 this.handleUserSettingsMatrixUserIdChange
586 <div class="form-group row">
587 <label class="col-lg-5 col-form-label" htmlFor="user-password">
588 {i18n.t('new_password')}
590 <div class="col-lg-7">
595 value={this.state.userSettingsForm.new_password}
596 autoComplete="new-password"
599 this.handleUserSettingsNewPasswordChange
604 <div class="form-group row">
606 class="col-lg-5 col-form-label"
607 htmlFor="user-verify-password"
609 {i18n.t('verify_password')}
611 <div class="col-lg-7">
614 id="user-verify-password"
616 value={this.state.userSettingsForm.new_password_verify}
617 autoComplete="new-password"
620 this.handleUserSettingsNewPasswordVerifyChange
625 <div class="form-group row">
627 class="col-lg-5 col-form-label"
628 htmlFor="user-old-password"
630 {i18n.t('old_password')}
632 <div class="col-lg-7">
635 id="user-old-password"
637 value={this.state.userSettingsForm.old_password}
638 autoComplete="new-password"
641 this.handleUserSettingsOldPasswordChange
646 {this.state.siteRes.site.enable_nsfw && (
647 <div class="form-group">
648 <div class="form-check">
650 class="form-check-input"
653 checked={this.state.userSettingsForm.show_nsfw}
656 this.handleUserSettingsShowNsfwChange
659 <label class="form-check-label" htmlFor="user-show-nsfw">
660 {i18n.t('show_nsfw')}
665 <div class="form-group">
666 <div class="form-check">
668 class="form-check-input"
669 id="user-show-avatars"
671 checked={this.state.userSettingsForm.show_avatars}
674 this.handleUserSettingsShowAvatarsChange
677 <label class="form-check-label" htmlFor="user-show-avatars">
678 {i18n.t('show_avatars')}
682 <div class="form-group">
683 <div class="form-check">
685 class="form-check-input"
686 id="user-send-notifications-to-email"
688 disabled={!this.state.user.email}
690 this.state.userSettingsForm.send_notifications_to_email
694 this.handleUserSettingsSendNotificationsToEmailChange
698 class="form-check-label"
699 htmlFor="user-send-notifications-to-email"
701 {i18n.t('send_notifications_to_email')}
705 <div class="form-group">
706 <button type="submit" class="btn btn-block btn-secondary mr-4">
707 {this.state.userSettingsLoading ? (
708 <svg class="icon icon-spinner spin">
709 <use xlinkHref="#icon-spinner"></use>
712 capitalizeFirstLetter(i18n.t('save'))
717 <div class="form-group mb-0">
719 class="btn btn-block btn-danger"
722 this.handleDeleteAccountShowConfirmToggle
725 {i18n.t('delete_account')}
727 {this.state.deleteAccountShowConfirm && (
729 <div class="my-2 alert alert-danger" role="alert">
730 {i18n.t('delete_account_confirm')}
734 value={this.state.deleteAccountForm.password}
735 autoComplete="new-password"
738 this.handleDeleteAccountPasswordChange
740 class="form-control my-2"
743 class="btn btn-danger mr-4"
744 disabled={!this.state.deleteAccountForm.password}
745 onClick={linkEvent(this, this.handleDeleteAccount)}
747 {this.state.deleteAccountLoading ? (
748 <svg class="icon icon-spinner spin">
749 <use xlinkHref="#icon-spinner"></use>
752 capitalizeFirstLetter(i18n.t('delete'))
756 class="btn btn-secondary"
759 this.handleDeleteAccountShowConfirmToggle
777 {this.state.moderates.length > 0 && (
778 <div class="card border-secondary mb-3">
779 <div class="card-body">
780 <h5>{i18n.t('moderates')}</h5>
781 <ul class="list-unstyled mb-0">
782 {this.state.moderates.map(community => (
784 <Link to={`/c/${community.community_name}`}>
785 {community.community_name}
800 {this.state.follows.length > 0 && (
801 <div class="card border-secondary mb-3">
802 <div class="card-body">
803 <h5>{i18n.t('subscribed')}</h5>
804 <ul class="list-unstyled mb-0">
805 {this.state.follows.map(community => (
807 <Link to={`/c/${community.community_name}`}>
808 {community.community_name}
820 updateUrl(paramUpdates: UrlParams) {
821 const page = paramUpdates.page || this.state.page;
823 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
825 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
826 this.props.history.push(
827 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
831 handlePageChange(page: number) {
832 this.updateUrl({ page });
835 handleSortChange(val: SortType) {
836 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
839 handleViewChange(i: User, event: any) {
841 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
846 handleUserSettingsShowNsfwChange(i: User, event: any) {
847 i.state.userSettingsForm.show_nsfw = event.target.checked;
851 handleUserSettingsShowAvatarsChange(i: User, event: any) {
852 i.state.userSettingsForm.show_avatars = event.target.checked;
853 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
857 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
858 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
862 handleUserSettingsThemeChange(i: User, event: any) {
863 i.state.userSettingsForm.theme = event.target.value;
864 setTheme(event.target.value, true);
868 handleUserSettingsLangChange(i: User, event: any) {
869 i.state.userSettingsForm.lang = event.target.value;
870 i18n.changeLanguage(i.state.userSettingsForm.lang);
874 handleUserSettingsSortTypeChange(val: SortType) {
875 this.state.userSettingsForm.default_sort_type = val;
876 this.setState(this.state);
879 handleUserSettingsListingTypeChange(val: ListingType) {
880 this.state.userSettingsForm.default_listing_type = val;
881 this.setState(this.state);
884 handleUserSettingsEmailChange(i: User, event: any) {
885 i.state.userSettingsForm.email = event.target.value;
886 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
887 i.state.userSettingsForm.email = undefined;
892 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
893 i.state.userSettingsForm.matrix_user_id = event.target.value;
895 i.state.userSettingsForm.matrix_user_id == '' &&
896 !i.state.user.matrix_user_id
898 i.state.userSettingsForm.matrix_user_id = undefined;
903 handleUserSettingsNewPasswordChange(i: User, event: any) {
904 i.state.userSettingsForm.new_password = event.target.value;
905 if (i.state.userSettingsForm.new_password == '') {
906 i.state.userSettingsForm.new_password = undefined;
911 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
912 i.state.userSettingsForm.new_password_verify = event.target.value;
913 if (i.state.userSettingsForm.new_password_verify == '') {
914 i.state.userSettingsForm.new_password_verify = undefined;
919 handleUserSettingsOldPasswordChange(i: User, event: any) {
920 i.state.userSettingsForm.old_password = event.target.value;
921 if (i.state.userSettingsForm.old_password == '') {
922 i.state.userSettingsForm.old_password = undefined;
927 handleImageUpload(i: User, event: any) {
928 event.preventDefault();
929 let file = event.target.files[0];
930 const imageUploadUrl = `/pictrs/image`;
931 const formData = new FormData();
932 formData.append('images[]', file);
934 i.state.avatarLoading = true;
937 fetch(imageUploadUrl, {
941 .then(res => res.json())
943 console.log('pictrs upload:');
945 if (res.msg == 'ok') {
946 let hash = res.files[0].file;
947 let url = `${window.location.origin}/pictrs/image/${hash}`;
948 i.state.userSettingsForm.avatar = url;
949 i.state.avatarLoading = false;
952 i.state.avatarLoading = false;
954 toast(JSON.stringify(res), 'danger');
958 i.state.avatarLoading = false;
960 toast(error, 'danger');
964 removeAvatar(i: User, event: any) {
965 event.preventDefault();
966 i.state.userSettingsLoading = true;
967 i.state.userSettingsForm.avatar = '';
970 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
973 get checkSettingsAvatar(): boolean {
975 this.state.userSettingsForm.avatar &&
976 this.state.userSettingsForm.avatar != ''
980 handleUserSettingsSubmit(i: User, event: any) {
981 event.preventDefault();
982 i.state.userSettingsLoading = true;
985 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
988 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
989 event.preventDefault();
990 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
994 handleDeleteAccountPasswordChange(i: User, event: any) {
995 i.state.deleteAccountForm.password = event.target.value;
999 handleLogoutClick(i: User) {
1000 UserService.Instance.logout();
1001 i.context.router.history.push('/');
1004 handleDeleteAccount(i: User, event: any) {
1005 event.preventDefault();
1006 i.state.deleteAccountLoading = true;
1007 i.setState(i.state);
1009 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1012 parseMessage(msg: WebSocketJsonResponse) {
1014 const res = wsJsonToRes(msg);
1016 toast(i18n.t(msg.error), 'danger');
1017 if (msg.error == 'couldnt_find_that_username_or_email') {
1018 this.context.router.history.push('/');
1021 deleteAccountLoading: false,
1022 avatarLoading: false,
1023 userSettingsLoading: false,
1026 } else if (res.op == UserOperation.GetUserDetails) {
1027 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1028 // and set the parent state if it is not set or differs
1029 const data = res.data as UserDetailsResponse;
1031 if (this.state.user.id !== data.user.id) {
1032 this.state.user = data.user;
1033 this.state.follows = data.follows;
1034 this.state.moderates = data.moderates;
1036 if (this.isCurrentUser) {
1037 this.state.userSettingsForm.show_nsfw =
1038 UserService.Instance.user.show_nsfw;
1039 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1040 ? UserService.Instance.user.theme
1042 this.state.userSettingsForm.default_sort_type =
1043 UserService.Instance.user.default_sort_type;
1044 this.state.userSettingsForm.default_listing_type =
1045 UserService.Instance.user.default_listing_type;
1046 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1047 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1048 this.state.userSettingsForm.email = this.state.user.email;
1049 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1050 this.state.userSettingsForm.show_avatars =
1051 UserService.Instance.user.show_avatars;
1052 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1054 this.state.loading = false;
1055 this.setState(this.state);
1057 } else if (res.op == UserOperation.SaveUserSettings) {
1058 const data = res.data as LoginResponse;
1059 UserService.Instance.login(data);
1061 userSettingsLoading: false,
1063 window.scrollTo(0, 0);
1064 } else if (res.op == UserOperation.DeleteAccount) {
1066 deleteAccountLoading: false,
1067 deleteAccountShowConfirm: false,
1069 this.context.router.history.push('/');
1070 } else if (res.op == UserOperation.GetSite) {
1071 const data = res.data as GetSiteResponse;
1072 this.state.siteRes = data;
1073 this.setState(this.state);
1074 } else if (res.op == UserOperation.AddAdmin) {
1075 const data = res.data as AddAdminResponse;
1076 this.state.siteRes.admins = data.admins;
1077 this.setState(this.state);