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;
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,
120 creator_id: undefined,
121 published: undefined,
122 creator_name: undefined,
123 number_of_users: undefined,
124 number_of_posts: undefined,
125 number_of_comments: undefined,
126 number_of_communities: undefined,
127 enable_downvotes: undefined,
128 open_registration: undefined,
129 enable_nsfw: undefined,
133 constructor(props: any, context: any) {
134 super(props, context);
136 this.state = this.emptyState;
137 this.handleSortChange = this.handleSortChange.bind(this);
138 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
141 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
144 this.handlePageChange = this.handlePageChange.bind(this);
146 this.state.user_id = Number(this.props.match.params.id) || null;
147 this.state.username = this.props.match.params.username;
149 this.subscription = WebSocketService.Instance.subject
150 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
152 msg => this.parseMessage(msg),
153 err => console.error(err),
154 () => console.log('complete')
157 WebSocketService.Instance.getSite();
160 get isCurrentUser() {
162 UserService.Instance.user &&
163 UserService.Instance.user.id == this.state.user.id
167 static getViewFromProps(view: any): UserDetailsView {
169 ? UserDetailsView[capitalizeFirstLetter(view)]
170 : UserDetailsView.Overview;
173 static getSortTypeFromProps(sort: any): SortType {
174 return sort ? routeSortTypeToEnum(sort) : SortType.New;
177 static getPageFromProps(page: any): number {
178 return page ? Number(page) : 1;
181 componentWillUnmount() {
182 this.subscription.unsubscribe();
185 static getDerivedStateFromProps(props: any): UserProps {
187 view: this.getViewFromProps(props.match.params.view),
188 sort: this.getSortTypeFromProps(props.match.params.sort),
189 page: this.getPageFromProps(props.match.params.page),
190 user_id: Number(props.match.params.id) || null,
191 username: props.match.params.username,
195 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
196 // Necessary if you are on a post and you click another post (same route)
198 lastProps.location.pathname.split('/')[2] !==
199 lastProps.history.location.pathname.split('/')[2]
201 // Couldnt get a refresh working. This does for now.
204 document.title = `/u/${this.state.username} - ${this.state.site.name}`;
210 <div class="container">
211 {this.state.loading ? (
213 <svg class="icon icon-spinner spin">
214 <use xlinkHref="#icon-spinner"></use>
219 <div class="col-12 col-md-8">
221 {this.state.user.avatar && showAvatars() && (
225 src={this.state.user.avatar}
226 class="rounded-circle mr-2"
229 <span>/u/{this.state.username}</span>
233 user_id={this.state.user_id}
234 username={this.state.username}
235 sort={SortType[this.state.sort]}
236 page={this.state.page}
238 enableDownvotes={this.state.site.enable_downvotes}
239 enableNsfw={this.state.site.enable_nsfw}
240 view={this.state.view}
241 onPageChange={this.handlePageChange}
244 <div class="col-12 col-md-4">
246 {this.isCurrentUser && this.userSettings()}
258 <div class="btn-group btn-group-toggle">
260 className={`btn btn-sm btn-secondary pointer btn-outline-light
261 ${this.state.view == UserDetailsView.Overview && 'active'}
266 value={UserDetailsView.Overview}
267 checked={this.state.view === UserDetailsView.Overview}
268 onChange={linkEvent(this, this.handleViewChange)}
273 className={`btn btn-sm btn-secondary pointer btn-outline-light
274 ${this.state.view == UserDetailsView.Comments && 'active'}
279 value={UserDetailsView.Comments}
280 checked={this.state.view == UserDetailsView.Comments}
281 onChange={linkEvent(this, this.handleViewChange)}
286 className={`btn btn-sm btn-secondary pointer btn-outline-light
287 ${this.state.view == UserDetailsView.Posts && 'active'}
292 value={UserDetailsView.Posts}
293 checked={this.state.view == UserDetailsView.Posts}
294 onChange={linkEvent(this, this.handleViewChange)}
299 className={`btn btn-sm btn-secondary pointer btn-outline-light
300 ${this.state.view == UserDetailsView.Saved && 'active'}
305 value={UserDetailsView.Saved}
306 checked={this.state.view == UserDetailsView.Saved}
307 onChange={linkEvent(this, this.handleViewChange)}
317 <div className="mb-2">
318 <span class="mr-3">{this.viewRadios()}</span>
320 sort={this.state.sort}
321 onChange={this.handleSortChange}
325 href={`/feeds/u/${this.state.username}.xml?sort=${
326 SortType[this.state.sort]
332 <svg class="icon mx-2 text-muted small">
333 <use xlinkHref="#icon-rss">#</use>
341 let user = this.state.user;
344 <div class="card border-secondary mb-3">
345 <div class="card-body">
347 <ul class="list-inline mb-0">
348 <li className="list-inline-item">
349 <UserListing user={user} realLink />
352 <li className="list-inline-item badge badge-danger">
358 <div className="d-flex align-items-center mb-2">
360 <use xlinkHref="#icon-cake"></use>
362 <span className="ml-2">
363 {i18n.t('cake_day_title')}{' '}
364 {moment.utc(user.published).local().format('MMM DD, YYYY')}
368 {i18n.t('joined')} <MomentTime data={user} showAgo />
370 <div class="table-responsive mt-1">
371 <table class="table table-bordered table-sm mt-2 mb-0">
374 <td class="text-center" colSpan={2}>
375 {i18n.t('number_of_points', {
376 count: user.post_score + user.comment_score,
384 {i18n.t('number_of_points', { count: user.post_score })}
388 {i18n.t('number_of_posts', { count: user.number_of_posts })}
394 {i18n.t('number_of_points', { count: user.comment_score })}
398 {i18n.t('number_of_comments', {
399 count: user.number_of_comments,
405 {this.isCurrentUser ? (
407 class="btn btn-block btn-secondary mt-3"
408 onClick={linkEvent(this, this.handleLogoutClick)}
415 className={`btn btn-block btn-secondary mt-3 ${
416 !this.state.user.matrix_user_id && 'disabled'
420 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
422 {i18n.t('send_secure_message')}
425 class="btn btn-block btn-secondary mt-3"
426 to={`/create_private_message?recipient_id=${this.state.user.id}`}
428 {i18n.t('send_message')}
441 <div class="card border-secondary mb-3">
442 <div class="card-body">
443 <h5>{i18n.t('settings')}</h5>
444 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
445 <div class="form-group">
446 <label>{i18n.t('avatar')}</label>
447 <form class="d-inline">
449 htmlFor="file-upload"
450 class="pointer ml-4 text-muted small font-weight-bold"
452 {!this.checkSettingsAvatar ? (
453 <span class="btn btn-sm btn-secondary">
454 {i18n.t('upload_avatar')}
460 src={this.state.userSettingsForm.avatar}
461 class="rounded-circle"
468 accept="image/*,video/*"
471 disabled={!UserService.Instance.user}
472 onChange={linkEvent(this, this.handleImageUpload)}
476 {this.checkSettingsAvatar && (
477 <div class="form-group">
479 class="btn btn-secondary btn-block"
480 onClick={linkEvent(this, this.removeAvatar)}
482 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
488 <div class="form-group">
489 <label>{i18n.t('language')}</label>
491 value={this.state.userSettingsForm.lang}
492 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
493 class="ml-2 custom-select custom-select-sm w-auto"
495 <option disabled>{i18n.t('language')}</option>
496 <option value="browser">{i18n.t('browser_default')}</option>
497 <option disabled>──</option>
498 {languages.map(lang => (
499 <option value={lang.code}>{lang.name}</option>
503 <div class="form-group">
504 <label>{i18n.t('theme')}</label>
506 value={this.state.userSettingsForm.theme}
507 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
508 class="ml-2 custom-select custom-select-sm w-auto"
510 <option disabled>{i18n.t('theme')}</option>
511 {themes.map(theme => (
512 <option value={theme}>{theme}</option>
516 <form className="form-group">
518 <div class="mr-2">{i18n.t('sort_type')}</div>
521 type_={this.state.userSettingsForm.default_listing_type}
522 onChange={this.handleUserSettingsListingTypeChange}
525 <form className="form-group">
527 <div class="mr-2">{i18n.t('type')}</div>
530 sort={this.state.userSettingsForm.default_sort_type}
531 onChange={this.handleUserSettingsSortTypeChange}
534 <div class="form-group row">
535 <label class="col-lg-3 col-form-label" htmlFor="user-email">
538 <div class="col-lg-9">
543 placeholder={i18n.t('optional')}
544 value={this.state.userSettingsForm.email}
547 this.handleUserSettingsEmailChange
553 <div class="form-group row">
554 <label class="col-lg-5 col-form-label">
556 href="https://about.riot.im/"
560 {i18n.t('matrix_user_id')}
563 <div class="col-lg-7">
567 placeholder="@user:example.com"
568 value={this.state.userSettingsForm.matrix_user_id}
571 this.handleUserSettingsMatrixUserIdChange
577 <div class="form-group row">
578 <label class="col-lg-5 col-form-label" htmlFor="user-password">
579 {i18n.t('new_password')}
581 <div class="col-lg-7">
586 value={this.state.userSettingsForm.new_password}
587 autoComplete="new-password"
590 this.handleUserSettingsNewPasswordChange
595 <div class="form-group row">
597 class="col-lg-5 col-form-label"
598 htmlFor="user-verify-password"
600 {i18n.t('verify_password')}
602 <div class="col-lg-7">
605 id="user-verify-password"
607 value={this.state.userSettingsForm.new_password_verify}
608 autoComplete="new-password"
611 this.handleUserSettingsNewPasswordVerifyChange
616 <div class="form-group row">
618 class="col-lg-5 col-form-label"
619 htmlFor="user-old-password"
621 {i18n.t('old_password')}
623 <div class="col-lg-7">
626 id="user-old-password"
628 value={this.state.userSettingsForm.old_password}
629 autoComplete="new-password"
632 this.handleUserSettingsOldPasswordChange
637 {this.state.site.enable_nsfw && (
638 <div class="form-group">
639 <div class="form-check">
641 class="form-check-input"
644 checked={this.state.userSettingsForm.show_nsfw}
647 this.handleUserSettingsShowNsfwChange
650 <label class="form-check-label" htmlFor="user-show-nsfw">
651 {i18n.t('show_nsfw')}
656 <div class="form-group">
657 <div class="form-check">
659 class="form-check-input"
660 id="user-show-avatars"
662 checked={this.state.userSettingsForm.show_avatars}
665 this.handleUserSettingsShowAvatarsChange
668 <label class="form-check-label" htmlFor="user-show-avatars">
669 {i18n.t('show_avatars')}
673 <div class="form-group">
674 <div class="form-check">
676 class="form-check-input"
677 id="user-send-notifications-to-email"
679 disabled={!this.state.user.email}
681 this.state.userSettingsForm.send_notifications_to_email
685 this.handleUserSettingsSendNotificationsToEmailChange
689 class="form-check-label"
690 htmlFor="user-send-notifications-to-email"
692 {i18n.t('send_notifications_to_email')}
696 <div class="form-group">
697 <button type="submit" class="btn btn-block btn-secondary mr-4">
698 {this.state.userSettingsLoading ? (
699 <svg class="icon icon-spinner spin">
700 <use xlinkHref="#icon-spinner"></use>
703 capitalizeFirstLetter(i18n.t('save'))
708 <div class="form-group mb-0">
710 class="btn btn-block btn-danger"
713 this.handleDeleteAccountShowConfirmToggle
716 {i18n.t('delete_account')}
718 {this.state.deleteAccountShowConfirm && (
720 <div class="my-2 alert alert-danger" role="alert">
721 {i18n.t('delete_account_confirm')}
725 value={this.state.deleteAccountForm.password}
726 autoComplete="new-password"
729 this.handleDeleteAccountPasswordChange
731 class="form-control my-2"
734 class="btn btn-danger mr-4"
735 disabled={!this.state.deleteAccountForm.password}
736 onClick={linkEvent(this, this.handleDeleteAccount)}
738 {this.state.deleteAccountLoading ? (
739 <svg class="icon icon-spinner spin">
740 <use xlinkHref="#icon-spinner"></use>
743 capitalizeFirstLetter(i18n.t('delete'))
747 class="btn btn-secondary"
750 this.handleDeleteAccountShowConfirmToggle
768 {this.state.moderates.length > 0 && (
769 <div class="card border-secondary mb-3">
770 <div class="card-body">
771 <h5>{i18n.t('moderates')}</h5>
772 <ul class="list-unstyled mb-0">
773 {this.state.moderates.map(community => (
775 <Link to={`/c/${community.community_name}`}>
776 {community.community_name}
791 {this.state.follows.length > 0 && (
792 <div class="card border-secondary mb-3">
793 <div class="card-body">
794 <h5>{i18n.t('subscribed')}</h5>
795 <ul class="list-unstyled mb-0">
796 {this.state.follows.map(community => (
798 <Link to={`/c/${community.community_name}`}>
799 {community.community_name}
811 updateUrl(paramUpdates: UrlParams) {
812 const page = paramUpdates.page || this.state.page;
814 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
816 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
817 this.props.history.push(
818 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
822 handlePageChange(page: number) {
823 this.updateUrl({ page });
826 handleSortChange(val: SortType) {
827 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
830 handleViewChange(i: User, event: any) {
832 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
837 handleUserSettingsShowNsfwChange(i: User, event: any) {
838 i.state.userSettingsForm.show_nsfw = event.target.checked;
842 handleUserSettingsShowAvatarsChange(i: User, event: any) {
843 i.state.userSettingsForm.show_avatars = event.target.checked;
844 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
848 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
849 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
853 handleUserSettingsThemeChange(i: User, event: any) {
854 i.state.userSettingsForm.theme = event.target.value;
855 setTheme(event.target.value, true);
859 handleUserSettingsLangChange(i: User, event: any) {
860 i.state.userSettingsForm.lang = event.target.value;
861 i18n.changeLanguage(i.state.userSettingsForm.lang);
865 handleUserSettingsSortTypeChange(val: SortType) {
866 this.state.userSettingsForm.default_sort_type = val;
867 this.setState(this.state);
870 handleUserSettingsListingTypeChange(val: ListingType) {
871 this.state.userSettingsForm.default_listing_type = val;
872 this.setState(this.state);
875 handleUserSettingsEmailChange(i: User, event: any) {
876 i.state.userSettingsForm.email = event.target.value;
877 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
878 i.state.userSettingsForm.email = undefined;
883 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
884 i.state.userSettingsForm.matrix_user_id = event.target.value;
886 i.state.userSettingsForm.matrix_user_id == '' &&
887 !i.state.user.matrix_user_id
889 i.state.userSettingsForm.matrix_user_id = undefined;
894 handleUserSettingsNewPasswordChange(i: User, event: any) {
895 i.state.userSettingsForm.new_password = event.target.value;
896 if (i.state.userSettingsForm.new_password == '') {
897 i.state.userSettingsForm.new_password = undefined;
902 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
903 i.state.userSettingsForm.new_password_verify = event.target.value;
904 if (i.state.userSettingsForm.new_password_verify == '') {
905 i.state.userSettingsForm.new_password_verify = undefined;
910 handleUserSettingsOldPasswordChange(i: User, event: any) {
911 i.state.userSettingsForm.old_password = event.target.value;
912 if (i.state.userSettingsForm.old_password == '') {
913 i.state.userSettingsForm.old_password = undefined;
918 handleImageUpload(i: User, event: any) {
919 event.preventDefault();
920 let file = event.target.files[0];
921 const imageUploadUrl = `/pictrs/image`;
922 const formData = new FormData();
923 formData.append('images[]', file);
925 i.state.avatarLoading = true;
928 fetch(imageUploadUrl, {
932 .then(res => res.json())
934 console.log('pictrs upload:');
936 if (res.msg == 'ok') {
937 let hash = res.files[0].file;
938 let url = `${window.location.origin}/pictrs/image/${hash}`;
939 i.state.userSettingsForm.avatar = url;
940 i.state.avatarLoading = false;
943 i.state.avatarLoading = false;
945 toast(JSON.stringify(res), 'danger');
949 i.state.avatarLoading = false;
951 toast(error, 'danger');
955 removeAvatar(i: User, event: any) {
956 event.preventDefault();
957 i.state.userSettingsLoading = true;
958 i.state.userSettingsForm.avatar = '';
961 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
964 get checkSettingsAvatar(): boolean {
966 this.state.userSettingsForm.avatar &&
967 this.state.userSettingsForm.avatar != ''
971 handleUserSettingsSubmit(i: User, event: any) {
972 event.preventDefault();
973 i.state.userSettingsLoading = true;
976 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
979 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
980 event.preventDefault();
981 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
985 handleDeleteAccountPasswordChange(i: User, event: any) {
986 i.state.deleteAccountForm.password = event.target.value;
990 handleLogoutClick(i: User) {
991 UserService.Instance.logout();
992 i.context.router.history.push('/');
995 handleDeleteAccount(i: User, event: any) {
996 event.preventDefault();
997 i.state.deleteAccountLoading = true;
1000 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1003 parseMessage(msg: WebSocketJsonResponse) {
1004 const res = wsJsonToRes(msg);
1006 toast(i18n.t(msg.error), 'danger');
1007 if (msg.error == 'couldnt_find_that_username_or_email') {
1008 this.context.router.history.push('/');
1011 deleteAccountLoading: false,
1012 avatarLoading: false,
1013 userSettingsLoading: false,
1016 } else if (res.op == UserOperation.GetUserDetails) {
1017 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1018 // and set the parent state if it is not set or differs
1019 const data = res.data as UserDetailsResponse;
1021 if (this.state.user.id !== data.user.id) {
1022 this.state.user = data.user;
1023 this.state.follows = data.follows;
1024 this.state.moderates = data.moderates;
1026 if (this.isCurrentUser) {
1027 this.state.userSettingsForm.show_nsfw =
1028 UserService.Instance.user.show_nsfw;
1029 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1030 ? UserService.Instance.user.theme
1032 this.state.userSettingsForm.default_sort_type =
1033 UserService.Instance.user.default_sort_type;
1034 this.state.userSettingsForm.default_listing_type =
1035 UserService.Instance.user.default_listing_type;
1036 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1037 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1038 this.state.userSettingsForm.email = this.state.user.email;
1039 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1040 this.state.userSettingsForm.show_avatars =
1041 UserService.Instance.user.show_avatars;
1042 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1044 this.setState(this.state);
1046 } else if (res.op == UserOperation.SaveUserSettings) {
1047 const data = res.data as LoginResponse;
1048 UserService.Instance.login(data);
1050 userSettingsLoading: false,
1052 window.scrollTo(0, 0);
1053 } else if (res.op == UserOperation.DeleteAccount) {
1055 deleteAccountLoading: false,
1056 deleteAccountShowConfirm: false,
1058 this.context.router.history.push('/');
1059 } else if (res.op == UserOperation.GetSite) {
1060 const data = res.data as GetSiteResponse;