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,
139 constructor(props: any, context: any) {
140 super(props, context);
142 this.state = this.emptyState;
143 this.handleSortChange = this.handleSortChange.bind(this);
144 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
147 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
150 this.handlePageChange = this.handlePageChange.bind(this);
152 this.state.user_id = Number(this.props.match.params.id) || null;
153 this.state.username = this.props.match.params.username;
155 this.subscription = WebSocketService.Instance.subject
156 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
158 msg => this.parseMessage(msg),
159 err => console.error(err),
160 () => console.log('complete')
163 WebSocketService.Instance.getSite();
166 get isCurrentUser() {
168 UserService.Instance.user &&
169 UserService.Instance.user.id == this.state.user.id
173 static getViewFromProps(view: any): UserDetailsView {
175 ? UserDetailsView[capitalizeFirstLetter(view)]
176 : UserDetailsView.Overview;
179 static getSortTypeFromProps(sort: any): SortType {
180 return sort ? routeSortTypeToEnum(sort) : SortType.New;
183 static getPageFromProps(page: any): number {
184 return page ? Number(page) : 1;
187 componentWillUnmount() {
188 this.subscription.unsubscribe();
191 static getDerivedStateFromProps(props: any): UserProps {
193 view: this.getViewFromProps(props.match.params.view),
194 sort: this.getSortTypeFromProps(props.match.params.sort),
195 page: this.getPageFromProps(props.match.params.page),
196 user_id: Number(props.match.params.id) || null,
197 username: props.match.params.username,
201 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
202 // Necessary if you are on a post and you click another post (same route)
204 lastProps.location.pathname.split('/')[2] !==
205 lastProps.history.location.pathname.split('/')[2]
207 // Couldnt get a refresh working. This does for now.
210 document.title = `/u/${this.state.username} - ${this.state.siteRes.site.name}`;
216 <div class="container">
218 <div class="col-12 col-md-8">
220 {this.state.user.avatar && showAvatars() && (
224 src={this.state.user.avatar}
225 class="rounded-circle mr-2"
228 <span>/u/{this.state.username}</span>
230 {this.state.loading ? (
232 <svg class="icon icon-spinner spin">
233 <use xlinkHref="#icon-spinner"></use>
240 user_id={this.state.user_id}
241 username={this.state.username}
242 sort={SortType[this.state.sort]}
243 page={this.state.page}
245 enableDownvotes={this.state.siteRes.site.enable_downvotes}
246 enableNsfw={this.state.siteRes.site.enable_nsfw}
247 admins={this.state.siteRes.admins}
248 view={this.state.view}
249 onPageChange={this.handlePageChange}
253 {!this.state.loading && (
254 <div class="col-12 col-md-4">
256 {this.isCurrentUser && this.userSettings()}
268 <div class="btn-group btn-group-toggle">
270 className={`btn btn-outline-secondary pointer
271 ${this.state.view == UserDetailsView.Overview && 'active'}
276 value={UserDetailsView.Overview}
277 checked={this.state.view === UserDetailsView.Overview}
278 onChange={linkEvent(this, this.handleViewChange)}
283 className={`btn btn-outline-secondary pointer
284 ${this.state.view == UserDetailsView.Comments && 'active'}
289 value={UserDetailsView.Comments}
290 checked={this.state.view == UserDetailsView.Comments}
291 onChange={linkEvent(this, this.handleViewChange)}
296 className={`btn btn-outline-secondary pointer
297 ${this.state.view == UserDetailsView.Posts && 'active'}
302 value={UserDetailsView.Posts}
303 checked={this.state.view == UserDetailsView.Posts}
304 onChange={linkEvent(this, this.handleViewChange)}
309 className={`btn btn-outline-secondary pointer
310 ${this.state.view == UserDetailsView.Saved && 'active'}
315 value={UserDetailsView.Saved}
316 checked={this.state.view == UserDetailsView.Saved}
317 onChange={linkEvent(this, this.handleViewChange)}
327 <div className="mb-2">
328 <span class="mr-3">{this.viewRadios()}</span>
330 sort={this.state.sort}
331 onChange={this.handleSortChange}
335 href={`/feeds/u/${this.state.username}.xml?sort=${
336 SortType[this.state.sort]
342 <svg class="icon mx-2 text-muted small">
343 <use xlinkHref="#icon-rss">#</use>
351 let user = this.state.user;
354 <div class="card bg-transparent border-secondary mb-3">
355 <div class="card-body">
357 <ul class="list-inline mb-0">
358 <li className="list-inline-item">
359 <UserListing user={user} realLink />
362 <li className="list-inline-item badge badge-danger">
368 <div className="d-flex align-items-center mb-2">
370 <use xlinkHref="#icon-cake"></use>
372 <span className="ml-2">
373 {i18n.t('cake_day_title')}{' '}
374 {moment.utc(user.published).local().format('MMM DD, YYYY')}
378 {i18n.t('joined')} <MomentTime data={user} showAgo />
380 <div class="table-responsive mt-1">
381 <table class="table table-bordered table-sm mt-2 mb-0">
384 <td class="text-center" colSpan={2}>
385 {i18n.t('number_of_points', {
386 count: user.post_score + user.comment_score,
394 {i18n.t('number_of_points', { count: user.post_score })}
398 {i18n.t('number_of_posts', { count: user.number_of_posts })}
404 {i18n.t('number_of_points', { count: user.comment_score })}
408 {i18n.t('number_of_comments', {
409 count: user.number_of_comments,
415 {this.isCurrentUser ? (
417 class="btn btn-block btn-secondary mt-3"
418 onClick={linkEvent(this, this.handleLogoutClick)}
425 className={`btn btn-block btn-secondary mt-3 ${
426 !this.state.user.matrix_user_id && 'disabled'
430 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
432 {i18n.t('send_secure_message')}
435 class="btn btn-block btn-secondary mt-3"
436 to={`/create_private_message?recipient_id=${this.state.user.id}`}
438 {i18n.t('send_message')}
451 <div class="card bg-transparent border-secondary mb-3">
452 <div class="card-body">
453 <h5>{i18n.t('settings')}</h5>
454 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
455 <div class="form-group">
456 <label>{i18n.t('avatar')}</label>
457 <form class="d-inline">
459 htmlFor="file-upload"
460 class="pointer ml-4 text-muted small font-weight-bold"
462 {!this.checkSettingsAvatar ? (
463 <span class="btn btn-secondary">
464 {i18n.t('upload_avatar')}
470 src={this.state.userSettingsForm.avatar}
471 class="rounded-circle"
478 accept="image/*,video/*"
481 disabled={!UserService.Instance.user}
482 onChange={linkEvent(this, this.handleImageUpload)}
486 {this.checkSettingsAvatar && (
487 <div class="form-group">
489 class="btn btn-secondary btn-block"
490 onClick={linkEvent(this, this.removeAvatar)}
492 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
498 <div class="form-group">
499 <label>{i18n.t('language')}</label>
501 value={this.state.userSettingsForm.lang}
502 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
503 class="ml-2 custom-select w-auto"
505 <option disabled>{i18n.t('language')}</option>
506 <option value="browser">{i18n.t('browser_default')}</option>
507 <option disabled>──</option>
508 {languages.map(lang => (
509 <option value={lang.code}>{lang.name}</option>
513 <div class="form-group">
514 <label>{i18n.t('theme')}</label>
516 value={this.state.userSettingsForm.theme}
517 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
518 class="ml-2 custom-select w-auto"
520 <option disabled>{i18n.t('theme')}</option>
521 {themes.map(theme => (
522 <option value={theme}>{theme}</option>
526 <form className="form-group">
528 <div class="mr-2">{i18n.t('sort_type')}</div>
531 type_={this.state.userSettingsForm.default_listing_type}
532 onChange={this.handleUserSettingsListingTypeChange}
535 <form className="form-group">
537 <div class="mr-2">{i18n.t('type')}</div>
540 sort={this.state.userSettingsForm.default_sort_type}
541 onChange={this.handleUserSettingsSortTypeChange}
544 <div class="form-group row">
545 <label class="col-lg-3 col-form-label" htmlFor="user-email">
548 <div class="col-lg-9">
553 placeholder={i18n.t('optional')}
554 value={this.state.userSettingsForm.email}
557 this.handleUserSettingsEmailChange
563 <div class="form-group row">
564 <label class="col-lg-5 col-form-label">
566 href="https://about.riot.im/"
570 {i18n.t('matrix_user_id')}
573 <div class="col-lg-7">
577 placeholder="@user:example.com"
578 value={this.state.userSettingsForm.matrix_user_id}
581 this.handleUserSettingsMatrixUserIdChange
587 <div class="form-group row">
588 <label class="col-lg-5 col-form-label" htmlFor="user-password">
589 {i18n.t('new_password')}
591 <div class="col-lg-7">
596 value={this.state.userSettingsForm.new_password}
597 autoComplete="new-password"
600 this.handleUserSettingsNewPasswordChange
605 <div class="form-group row">
607 class="col-lg-5 col-form-label"
608 htmlFor="user-verify-password"
610 {i18n.t('verify_password')}
612 <div class="col-lg-7">
615 id="user-verify-password"
617 value={this.state.userSettingsForm.new_password_verify}
618 autoComplete="new-password"
621 this.handleUserSettingsNewPasswordVerifyChange
626 <div class="form-group row">
628 class="col-lg-5 col-form-label"
629 htmlFor="user-old-password"
631 {i18n.t('old_password')}
633 <div class="col-lg-7">
636 id="user-old-password"
638 value={this.state.userSettingsForm.old_password}
639 autoComplete="new-password"
642 this.handleUserSettingsOldPasswordChange
647 {this.state.siteRes.site.enable_nsfw && (
648 <div class="form-group">
649 <div class="form-check">
651 class="form-check-input"
654 checked={this.state.userSettingsForm.show_nsfw}
657 this.handleUserSettingsShowNsfwChange
660 <label class="form-check-label" htmlFor="user-show-nsfw">
661 {i18n.t('show_nsfw')}
666 <div class="form-group">
667 <div class="form-check">
669 class="form-check-input"
670 id="user-show-avatars"
672 checked={this.state.userSettingsForm.show_avatars}
675 this.handleUserSettingsShowAvatarsChange
678 <label class="form-check-label" htmlFor="user-show-avatars">
679 {i18n.t('show_avatars')}
683 <div class="form-group">
684 <div class="form-check">
686 class="form-check-input"
687 id="user-send-notifications-to-email"
689 disabled={!this.state.user.email}
691 this.state.userSettingsForm.send_notifications_to_email
695 this.handleUserSettingsSendNotificationsToEmailChange
699 class="form-check-label"
700 htmlFor="user-send-notifications-to-email"
702 {i18n.t('send_notifications_to_email')}
706 <div class="form-group">
707 <button type="submit" class="btn btn-block btn-secondary mr-4">
708 {this.state.userSettingsLoading ? (
709 <svg class="icon icon-spinner spin">
710 <use xlinkHref="#icon-spinner"></use>
713 capitalizeFirstLetter(i18n.t('save'))
718 <div class="form-group mb-0">
720 class="btn btn-block btn-danger"
723 this.handleDeleteAccountShowConfirmToggle
726 {i18n.t('delete_account')}
728 {this.state.deleteAccountShowConfirm && (
730 <div class="my-2 alert alert-danger" role="alert">
731 {i18n.t('delete_account_confirm')}
735 value={this.state.deleteAccountForm.password}
736 autoComplete="new-password"
739 this.handleDeleteAccountPasswordChange
741 class="form-control my-2"
744 class="btn btn-danger mr-4"
745 disabled={!this.state.deleteAccountForm.password}
746 onClick={linkEvent(this, this.handleDeleteAccount)}
748 {this.state.deleteAccountLoading ? (
749 <svg class="icon icon-spinner spin">
750 <use xlinkHref="#icon-spinner"></use>
753 capitalizeFirstLetter(i18n.t('delete'))
757 class="btn btn-secondary"
760 this.handleDeleteAccountShowConfirmToggle
778 {this.state.moderates.length > 0 && (
779 <div class="card bg-transparent border-secondary mb-3">
780 <div class="card-body">
781 <h5>{i18n.t('moderates')}</h5>
782 <ul class="list-unstyled mb-0">
783 {this.state.moderates.map(community => (
785 <Link to={`/c/${community.community_name}`}>
786 {community.community_name}
801 {this.state.follows.length > 0 && (
802 <div class="card bg-transparent border-secondary mb-3">
803 <div class="card-body">
804 <h5>{i18n.t('subscribed')}</h5>
805 <ul class="list-unstyled mb-0">
806 {this.state.follows.map(community => (
808 <Link to={`/c/${community.community_name}`}>
809 {community.community_name}
821 updateUrl(paramUpdates: UrlParams) {
822 const page = paramUpdates.page || this.state.page;
824 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
826 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
827 this.props.history.push(
828 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
832 handlePageChange(page: number) {
833 this.updateUrl({ page });
836 handleSortChange(val: SortType) {
837 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
840 handleViewChange(i: User, event: any) {
842 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
847 handleUserSettingsShowNsfwChange(i: User, event: any) {
848 i.state.userSettingsForm.show_nsfw = event.target.checked;
852 handleUserSettingsShowAvatarsChange(i: User, event: any) {
853 i.state.userSettingsForm.show_avatars = event.target.checked;
854 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
858 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
859 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
863 handleUserSettingsThemeChange(i: User, event: any) {
864 i.state.userSettingsForm.theme = event.target.value;
865 setTheme(event.target.value, true);
869 handleUserSettingsLangChange(i: User, event: any) {
870 i.state.userSettingsForm.lang = event.target.value;
871 i18n.changeLanguage(i.state.userSettingsForm.lang);
875 handleUserSettingsSortTypeChange(val: SortType) {
876 this.state.userSettingsForm.default_sort_type = val;
877 this.setState(this.state);
880 handleUserSettingsListingTypeChange(val: ListingType) {
881 this.state.userSettingsForm.default_listing_type = val;
882 this.setState(this.state);
885 handleUserSettingsEmailChange(i: User, event: any) {
886 i.state.userSettingsForm.email = event.target.value;
887 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
888 i.state.userSettingsForm.email = undefined;
893 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
894 i.state.userSettingsForm.matrix_user_id = event.target.value;
896 i.state.userSettingsForm.matrix_user_id == '' &&
897 !i.state.user.matrix_user_id
899 i.state.userSettingsForm.matrix_user_id = undefined;
904 handleUserSettingsNewPasswordChange(i: User, event: any) {
905 i.state.userSettingsForm.new_password = event.target.value;
906 if (i.state.userSettingsForm.new_password == '') {
907 i.state.userSettingsForm.new_password = undefined;
912 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
913 i.state.userSettingsForm.new_password_verify = event.target.value;
914 if (i.state.userSettingsForm.new_password_verify == '') {
915 i.state.userSettingsForm.new_password_verify = undefined;
920 handleUserSettingsOldPasswordChange(i: User, event: any) {
921 i.state.userSettingsForm.old_password = event.target.value;
922 if (i.state.userSettingsForm.old_password == '') {
923 i.state.userSettingsForm.old_password = undefined;
928 handleImageUpload(i: User, event: any) {
929 event.preventDefault();
930 let file = event.target.files[0];
931 const imageUploadUrl = `/pictrs/image`;
932 const formData = new FormData();
933 formData.append('images[]', file);
935 i.state.avatarLoading = true;
938 fetch(imageUploadUrl, {
942 .then(res => res.json())
944 console.log('pictrs upload:');
946 if (res.msg == 'ok') {
947 let hash = res.files[0].file;
948 let url = `${window.location.origin}/pictrs/image/${hash}`;
949 i.state.userSettingsForm.avatar = url;
950 i.state.avatarLoading = false;
953 i.state.avatarLoading = false;
955 toast(JSON.stringify(res), 'danger');
959 i.state.avatarLoading = false;
961 toast(error, 'danger');
965 removeAvatar(i: User, event: any) {
966 event.preventDefault();
967 i.state.userSettingsLoading = true;
968 i.state.userSettingsForm.avatar = '';
971 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
974 get checkSettingsAvatar(): boolean {
976 this.state.userSettingsForm.avatar &&
977 this.state.userSettingsForm.avatar != ''
981 handleUserSettingsSubmit(i: User, event: any) {
982 event.preventDefault();
983 i.state.userSettingsLoading = true;
986 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
989 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
990 event.preventDefault();
991 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
995 handleDeleteAccountPasswordChange(i: User, event: any) {
996 i.state.deleteAccountForm.password = event.target.value;
1000 handleLogoutClick(i: User) {
1001 UserService.Instance.logout();
1002 i.context.router.history.push('/');
1005 handleDeleteAccount(i: User, event: any) {
1006 event.preventDefault();
1007 i.state.deleteAccountLoading = true;
1008 i.setState(i.state);
1010 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1013 parseMessage(msg: WebSocketJsonResponse) {
1015 const res = wsJsonToRes(msg);
1017 toast(i18n.t(msg.error), 'danger');
1018 if (msg.error == 'couldnt_find_that_username_or_email') {
1019 this.context.router.history.push('/');
1022 deleteAccountLoading: false,
1023 avatarLoading: false,
1024 userSettingsLoading: false,
1027 } else if (res.op == UserOperation.GetUserDetails) {
1028 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1029 // and set the parent state if it is not set or differs
1030 const data = res.data as UserDetailsResponse;
1032 if (this.state.user.id !== data.user.id) {
1033 this.state.user = data.user;
1034 this.state.follows = data.follows;
1035 this.state.moderates = data.moderates;
1037 if (this.isCurrentUser) {
1038 this.state.userSettingsForm.show_nsfw =
1039 UserService.Instance.user.show_nsfw;
1040 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1041 ? UserService.Instance.user.theme
1043 this.state.userSettingsForm.default_sort_type =
1044 UserService.Instance.user.default_sort_type;
1045 this.state.userSettingsForm.default_listing_type =
1046 UserService.Instance.user.default_listing_type;
1047 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1048 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1049 this.state.userSettingsForm.email = this.state.user.email;
1050 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1051 this.state.userSettingsForm.show_avatars =
1052 UserService.Instance.user.show_avatars;
1053 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1055 this.state.loading = false;
1056 this.setState(this.state);
1058 } else if (res.op == UserOperation.SaveUserSettings) {
1059 const data = res.data as LoginResponse;
1060 UserService.Instance.login(data);
1062 userSettingsLoading: false,
1064 window.scrollTo(0, 0);
1065 } else if (res.op == UserOperation.DeleteAccount) {
1067 deleteAccountLoading: false,
1068 deleteAccountShowConfirm: false,
1070 this.context.router.history.push('/');
1071 } else if (res.op == UserOperation.GetSite) {
1072 const data = res.data as GetSiteResponse;
1073 this.state.siteRes = data;
1074 this.setState(this.state);
1075 } else if (res.op == UserOperation.AddAdmin) {
1076 const data = res.data as AddAdminResponse;
1077 this.state.siteRes.admins = data.admins;
1078 this.setState(this.state);