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,
35 import { UserListing } from './user-listing';
36 import { SortSelect } from './sort-select';
37 import { ListingTypeSelect } from './listing-type-select';
38 import { MomentTime } from './moment-time';
39 import { i18n } from '../i18next';
40 import moment from 'moment';
41 import { UserDetails } from './user-details';
47 follows: Array<CommunityUser>;
48 moderates: Array<CommunityUser>;
49 view: UserDetailsView;
53 avatarLoading: boolean;
54 userSettingsForm: UserSettingsForm;
55 userSettingsLoading: boolean;
56 deleteAccountLoading: boolean;
57 deleteAccountShowConfirm: boolean;
58 deleteAccountForm: DeleteAccountForm;
59 siteRes: GetSiteResponse;
63 view: UserDetailsView;
66 user_id: number | null;
76 export class User extends Component<any, UserState> {
77 private subscription: Subscription;
78 private emptyState: UserState = {
83 number_of_posts: null,
85 number_of_comments: null,
90 send_notifications_to_email: null,
100 view: User.getViewFromProps(this.props.match.view),
101 sort: User.getSortTypeFromProps(this.props.match.sort),
102 page: User.getPageFromProps(this.props.match.page),
106 default_sort_type: null,
107 default_listing_type: null,
110 send_notifications_to_email: null,
113 userSettingsLoading: null,
114 deleteAccountLoading: null,
115 deleteAccountShowConfirm: false,
126 creator_id: undefined,
127 published: undefined,
128 creator_name: undefined,
129 number_of_users: undefined,
130 number_of_posts: undefined,
131 number_of_comments: undefined,
132 number_of_communities: undefined,
133 enable_downvotes: undefined,
134 open_registration: undefined,
135 enable_nsfw: undefined,
141 constructor(props: any, context: any) {
142 super(props, context);
144 this.state = this.emptyState;
145 this.handleSortChange = this.handleSortChange.bind(this);
146 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
149 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
152 this.handlePageChange = this.handlePageChange.bind(this);
154 this.state.user_id = Number(this.props.match.params.id) || null;
155 this.state.username = this.props.match.params.username;
157 this.subscription = WebSocketService.Instance.subject
158 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
160 msg => this.parseMessage(msg),
161 err => console.error(err),
162 () => console.log('complete')
165 WebSocketService.Instance.getSite();
168 get isCurrentUser() {
170 UserService.Instance.user &&
171 UserService.Instance.user.id == this.state.user.id
175 static getViewFromProps(view: any): UserDetailsView {
177 ? UserDetailsView[capitalizeFirstLetter(view)]
178 : UserDetailsView.Overview;
181 static getSortTypeFromProps(sort: any): SortType {
182 return sort ? routeSortTypeToEnum(sort) : SortType.New;
185 static getPageFromProps(page: any): number {
186 return page ? Number(page) : 1;
189 componentWillUnmount() {
190 this.subscription.unsubscribe();
193 static getDerivedStateFromProps(props: any): UserProps {
195 view: this.getViewFromProps(props.match.params.view),
196 sort: this.getSortTypeFromProps(props.match.params.sort),
197 page: this.getPageFromProps(props.match.params.page),
198 user_id: Number(props.match.params.id) || null,
199 username: props.match.params.username,
203 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
204 // Necessary if you are on a post and you click another post (same route)
206 lastProps.location.pathname.split('/')[2] !==
207 lastProps.history.location.pathname.split('/')[2]
209 // Couldnt get a refresh working. This does for now.
215 get documentTitle(): string {
216 if (this.state.siteRes.site.name) {
217 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
225 <div class="container">
226 <Helmet title={this.documentTitle} />
228 <div class="col-12 col-md-8">
230 {this.state.user.avatar && showAvatars() && (
234 src={this.state.user.avatar}
235 class="rounded-circle mr-2"
238 <span>@{this.state.username}</span>
240 {this.state.loading ? (
242 <svg class="icon icon-spinner spin">
243 <use xlinkHref="#icon-spinner"></use>
250 user_id={this.state.user_id}
251 username={this.state.username}
252 sort={SortType[this.state.sort]}
253 page={this.state.page}
255 enableDownvotes={this.state.siteRes.site.enable_downvotes}
256 enableNsfw={this.state.siteRes.site.enable_nsfw}
257 admins={this.state.siteRes.admins}
258 view={this.state.view}
259 onPageChange={this.handlePageChange}
263 {!this.state.loading && (
264 <div class="col-12 col-md-4">
266 {this.isCurrentUser && this.userSettings()}
278 <div class="btn-group btn-group-toggle flex-wrap mb-2">
280 className={`btn btn-outline-secondary pointer
281 ${this.state.view == UserDetailsView.Overview && 'active'}
286 value={UserDetailsView.Overview}
287 checked={this.state.view === UserDetailsView.Overview}
288 onChange={linkEvent(this, this.handleViewChange)}
293 className={`btn btn-outline-secondary pointer
294 ${this.state.view == UserDetailsView.Comments && 'active'}
299 value={UserDetailsView.Comments}
300 checked={this.state.view == UserDetailsView.Comments}
301 onChange={linkEvent(this, this.handleViewChange)}
306 className={`btn btn-outline-secondary pointer
307 ${this.state.view == UserDetailsView.Posts && 'active'}
312 value={UserDetailsView.Posts}
313 checked={this.state.view == UserDetailsView.Posts}
314 onChange={linkEvent(this, this.handleViewChange)}
319 className={`btn btn-outline-secondary pointer
320 ${this.state.view == UserDetailsView.Saved && 'active'}
325 value={UserDetailsView.Saved}
326 checked={this.state.view == UserDetailsView.Saved}
327 onChange={linkEvent(this, this.handleViewChange)}
337 <div className="mb-2">
338 <span class="mr-3">{this.viewRadios()}</span>
340 sort={this.state.sort}
341 onChange={this.handleSortChange}
345 href={`/feeds/u/${this.state.username}.xml?sort=${
346 SortType[this.state.sort]
352 <svg class="icon mx-2 text-muted small">
353 <use xlinkHref="#icon-rss">#</use>
361 let user = this.state.user;
364 <div class="card bg-transparent border-secondary mb-3">
365 <div class="card-body">
367 <ul class="list-inline mb-0">
368 <li className="list-inline-item">
369 <UserListing user={user} realLink />
372 <li className="list-inline-item badge badge-danger">
378 <div className="d-flex align-items-center mb-2">
380 <use xlinkHref="#icon-cake"></use>
382 <span className="ml-2">
383 {i18n.t('cake_day_title')}{' '}
384 {moment.utc(user.published).local().format('MMM DD, YYYY')}
388 {i18n.t('joined')} <MomentTime data={user} showAgo />
390 <div class="table-responsive mt-1">
391 <table class="table table-bordered table-sm mt-2 mb-0">
394 <td class="text-center" colSpan={2}>
395 {i18n.t('number_of_points', {
396 count: user.post_score + user.comment_score,
404 {i18n.t('number_of_points', { count: user.post_score })}
408 {i18n.t('number_of_posts', { count: user.number_of_posts })}
414 {i18n.t('number_of_points', { count: user.comment_score })}
418 {i18n.t('number_of_comments', {
419 count: user.number_of_comments,
425 {this.isCurrentUser ? (
427 class="btn btn-block btn-secondary mt-3"
428 onClick={linkEvent(this, this.handleLogoutClick)}
435 className={`btn btn-block btn-secondary mt-3 ${
436 !this.state.user.matrix_user_id && 'disabled'
440 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
442 {i18n.t('send_secure_message')}
445 class="btn btn-block btn-secondary mt-3"
446 to={`/create_private_message?recipient_id=${this.state.user.id}`}
448 {i18n.t('send_message')}
461 <div class="card bg-transparent border-secondary mb-3">
462 <div class="card-body">
463 <h5>{i18n.t('settings')}</h5>
464 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
465 <div class="form-group">
466 <label>{i18n.t('avatar')}</label>
467 <form class="d-inline">
469 htmlFor="file-upload"
470 class="pointer ml-4 text-muted small font-weight-bold"
472 {!this.checkSettingsAvatar ? (
473 <span class="btn btn-secondary">
474 {i18n.t('upload_avatar')}
480 src={this.state.userSettingsForm.avatar}
481 class="rounded-circle"
488 accept="image/*,video/*"
491 disabled={!UserService.Instance.user}
492 onChange={linkEvent(this, this.handleImageUpload)}
496 {this.checkSettingsAvatar && (
497 <div class="form-group">
499 class="btn btn-secondary btn-block"
500 onClick={linkEvent(this, this.removeAvatar)}
502 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
508 <div class="form-group">
509 <label>{i18n.t('language')}</label>
511 value={this.state.userSettingsForm.lang}
512 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
513 class="ml-2 custom-select w-auto"
515 <option disabled>{i18n.t('language')}</option>
516 <option value="browser">{i18n.t('browser_default')}</option>
517 <option disabled>──</option>
518 {languages.map(lang => (
519 <option value={lang.code}>{lang.name}</option>
523 <div class="form-group">
524 <label>{i18n.t('theme')}</label>
526 value={this.state.userSettingsForm.theme}
527 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
528 class="ml-2 custom-select w-auto"
530 <option disabled>{i18n.t('theme')}</option>
531 {themes.map(theme => (
532 <option value={theme}>{theme}</option>
536 <form className="form-group">
538 <div class="mr-2">{i18n.t('sort_type')}</div>
541 type_={this.state.userSettingsForm.default_listing_type}
542 onChange={this.handleUserSettingsListingTypeChange}
545 <form className="form-group">
547 <div class="mr-2">{i18n.t('type')}</div>
550 sort={this.state.userSettingsForm.default_sort_type}
551 onChange={this.handleUserSettingsSortTypeChange}
554 <div class="form-group row">
555 <label class="col-lg-3 col-form-label" htmlFor="user-email">
558 <div class="col-lg-9">
563 placeholder={i18n.t('optional')}
564 value={this.state.userSettingsForm.email}
567 this.handleUserSettingsEmailChange
573 <div class="form-group row">
574 <label class="col-lg-5 col-form-label">
576 href="https://about.riot.im/"
580 {i18n.t('matrix_user_id')}
583 <div class="col-lg-7">
587 placeholder="@user:example.com"
588 value={this.state.userSettingsForm.matrix_user_id}
591 this.handleUserSettingsMatrixUserIdChange
597 <div class="form-group row">
598 <label class="col-lg-5 col-form-label" htmlFor="user-password">
599 {i18n.t('new_password')}
601 <div class="col-lg-7">
606 value={this.state.userSettingsForm.new_password}
607 autoComplete="new-password"
610 this.handleUserSettingsNewPasswordChange
615 <div class="form-group row">
617 class="col-lg-5 col-form-label"
618 htmlFor="user-verify-password"
620 {i18n.t('verify_password')}
622 <div class="col-lg-7">
625 id="user-verify-password"
627 value={this.state.userSettingsForm.new_password_verify}
628 autoComplete="new-password"
631 this.handleUserSettingsNewPasswordVerifyChange
636 <div class="form-group row">
638 class="col-lg-5 col-form-label"
639 htmlFor="user-old-password"
641 {i18n.t('old_password')}
643 <div class="col-lg-7">
646 id="user-old-password"
648 value={this.state.userSettingsForm.old_password}
649 autoComplete="new-password"
652 this.handleUserSettingsOldPasswordChange
657 {this.state.siteRes.site.enable_nsfw && (
658 <div class="form-group">
659 <div class="form-check">
661 class="form-check-input"
664 checked={this.state.userSettingsForm.show_nsfw}
667 this.handleUserSettingsShowNsfwChange
670 <label class="form-check-label" htmlFor="user-show-nsfw">
671 {i18n.t('show_nsfw')}
676 <div class="form-group">
677 <div class="form-check">
679 class="form-check-input"
680 id="user-show-avatars"
682 checked={this.state.userSettingsForm.show_avatars}
685 this.handleUserSettingsShowAvatarsChange
688 <label class="form-check-label" htmlFor="user-show-avatars">
689 {i18n.t('show_avatars')}
693 <div class="form-group">
694 <div class="form-check">
696 class="form-check-input"
697 id="user-send-notifications-to-email"
699 disabled={!this.state.user.email}
701 this.state.userSettingsForm.send_notifications_to_email
705 this.handleUserSettingsSendNotificationsToEmailChange
709 class="form-check-label"
710 htmlFor="user-send-notifications-to-email"
712 {i18n.t('send_notifications_to_email')}
716 <div class="form-group">
717 <button type="submit" class="btn btn-block btn-secondary mr-4">
718 {this.state.userSettingsLoading ? (
719 <svg class="icon icon-spinner spin">
720 <use xlinkHref="#icon-spinner"></use>
723 capitalizeFirstLetter(i18n.t('save'))
728 <div class="form-group mb-0">
730 class="btn btn-block btn-danger"
733 this.handleDeleteAccountShowConfirmToggle
736 {i18n.t('delete_account')}
738 {this.state.deleteAccountShowConfirm && (
740 <div class="my-2 alert alert-danger" role="alert">
741 {i18n.t('delete_account_confirm')}
745 value={this.state.deleteAccountForm.password}
746 autoComplete="new-password"
749 this.handleDeleteAccountPasswordChange
751 class="form-control my-2"
754 class="btn btn-danger mr-4"
755 disabled={!this.state.deleteAccountForm.password}
756 onClick={linkEvent(this, this.handleDeleteAccount)}
758 {this.state.deleteAccountLoading ? (
759 <svg class="icon icon-spinner spin">
760 <use xlinkHref="#icon-spinner"></use>
763 capitalizeFirstLetter(i18n.t('delete'))
767 class="btn btn-secondary"
770 this.handleDeleteAccountShowConfirmToggle
788 {this.state.moderates.length > 0 && (
789 <div class="card bg-transparent border-secondary mb-3">
790 <div class="card-body">
791 <h5>{i18n.t('moderates')}</h5>
792 <ul class="list-unstyled mb-0">
793 {this.state.moderates.map(community => (
795 <Link to={`/c/${community.community_name}`}>
796 {community.community_name}
811 {this.state.follows.length > 0 && (
812 <div class="card bg-transparent border-secondary mb-3">
813 <div class="card-body">
814 <h5>{i18n.t('subscribed')}</h5>
815 <ul class="list-unstyled mb-0">
816 {this.state.follows.map(community => (
818 <Link to={`/c/${community.community_name}`}>
819 {community.community_name}
831 updateUrl(paramUpdates: UrlParams) {
832 const page = paramUpdates.page || this.state.page;
834 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
836 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
837 this.props.history.push(
838 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
842 handlePageChange(page: number) {
843 this.updateUrl({ page });
846 handleSortChange(val: SortType) {
847 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
850 handleViewChange(i: User, event: any) {
852 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
857 handleUserSettingsShowNsfwChange(i: User, event: any) {
858 i.state.userSettingsForm.show_nsfw = event.target.checked;
862 handleUserSettingsShowAvatarsChange(i: User, event: any) {
863 i.state.userSettingsForm.show_avatars = event.target.checked;
864 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
868 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
869 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
873 handleUserSettingsThemeChange(i: User, event: any) {
874 i.state.userSettingsForm.theme = event.target.value;
875 setTheme(event.target.value, true);
879 handleUserSettingsLangChange(i: User, event: any) {
880 i.state.userSettingsForm.lang = event.target.value;
881 i18n.changeLanguage(getLanguage(i.state.userSettingsForm.lang));
885 handleUserSettingsSortTypeChange(val: SortType) {
886 this.state.userSettingsForm.default_sort_type = val;
887 this.setState(this.state);
890 handleUserSettingsListingTypeChange(val: ListingType) {
891 this.state.userSettingsForm.default_listing_type = val;
892 this.setState(this.state);
895 handleUserSettingsEmailChange(i: User, event: any) {
896 i.state.userSettingsForm.email = event.target.value;
897 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
898 i.state.userSettingsForm.email = undefined;
903 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
904 i.state.userSettingsForm.matrix_user_id = event.target.value;
906 i.state.userSettingsForm.matrix_user_id == '' &&
907 !i.state.user.matrix_user_id
909 i.state.userSettingsForm.matrix_user_id = undefined;
914 handleUserSettingsNewPasswordChange(i: User, event: any) {
915 i.state.userSettingsForm.new_password = event.target.value;
916 if (i.state.userSettingsForm.new_password == '') {
917 i.state.userSettingsForm.new_password = undefined;
922 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
923 i.state.userSettingsForm.new_password_verify = event.target.value;
924 if (i.state.userSettingsForm.new_password_verify == '') {
925 i.state.userSettingsForm.new_password_verify = undefined;
930 handleUserSettingsOldPasswordChange(i: User, event: any) {
931 i.state.userSettingsForm.old_password = event.target.value;
932 if (i.state.userSettingsForm.old_password == '') {
933 i.state.userSettingsForm.old_password = undefined;
938 handleImageUpload(i: User, event: any) {
939 event.preventDefault();
940 let file = event.target.files[0];
941 const imageUploadUrl = `/pictrs/image`;
942 const formData = new FormData();
943 formData.append('images[]', file);
945 i.state.avatarLoading = true;
948 fetch(imageUploadUrl, {
952 .then(res => res.json())
954 console.log('pictrs upload:');
956 if (res.msg == 'ok') {
957 let hash = res.files[0].file;
958 let url = `${window.location.origin}/pictrs/image/${hash}`;
959 i.state.userSettingsForm.avatar = url;
960 i.state.avatarLoading = false;
963 i.state.avatarLoading = false;
965 toast(JSON.stringify(res), 'danger');
969 i.state.avatarLoading = false;
971 toast(error, 'danger');
975 removeAvatar(i: User, event: any) {
976 event.preventDefault();
977 i.state.userSettingsLoading = true;
978 i.state.userSettingsForm.avatar = '';
981 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
984 get checkSettingsAvatar(): boolean {
986 this.state.userSettingsForm.avatar &&
987 this.state.userSettingsForm.avatar != ''
991 handleUserSettingsSubmit(i: User, event: any) {
992 event.preventDefault();
993 i.state.userSettingsLoading = true;
996 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
999 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1000 event.preventDefault();
1001 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1002 i.setState(i.state);
1005 handleDeleteAccountPasswordChange(i: User, event: any) {
1006 i.state.deleteAccountForm.password = event.target.value;
1007 i.setState(i.state);
1010 handleLogoutClick(i: User) {
1011 UserService.Instance.logout();
1012 i.context.router.history.push('/');
1015 handleDeleteAccount(i: User, event: any) {
1016 event.preventDefault();
1017 i.state.deleteAccountLoading = true;
1018 i.setState(i.state);
1020 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1023 parseMessage(msg: WebSocketJsonResponse) {
1025 const res = wsJsonToRes(msg);
1027 toast(i18n.t(msg.error), 'danger');
1028 if (msg.error == 'couldnt_find_that_username_or_email') {
1029 this.context.router.history.push('/');
1032 deleteAccountLoading: false,
1033 avatarLoading: false,
1034 userSettingsLoading: false,
1037 } else if (res.op == UserOperation.GetUserDetails) {
1038 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1039 // and set the parent state if it is not set or differs
1040 const data = res.data as UserDetailsResponse;
1042 if (this.state.user.id !== data.user.id) {
1043 this.state.user = data.user;
1044 this.state.follows = data.follows;
1045 this.state.moderates = data.moderates;
1047 if (this.isCurrentUser) {
1048 this.state.userSettingsForm.show_nsfw =
1049 UserService.Instance.user.show_nsfw;
1050 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1051 ? UserService.Instance.user.theme
1053 this.state.userSettingsForm.default_sort_type =
1054 UserService.Instance.user.default_sort_type;
1055 this.state.userSettingsForm.default_listing_type =
1056 UserService.Instance.user.default_listing_type;
1057 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1058 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1059 this.state.userSettingsForm.email = this.state.user.email;
1060 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1061 this.state.userSettingsForm.show_avatars =
1062 UserService.Instance.user.show_avatars;
1063 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1065 this.state.loading = false;
1066 this.setState(this.state);
1068 } else if (res.op == UserOperation.SaveUserSettings) {
1069 const data = res.data as LoginResponse;
1070 UserService.Instance.login(data);
1072 userSettingsLoading: false,
1074 window.scrollTo(0, 0);
1075 } else if (res.op == UserOperation.DeleteAccount) {
1077 deleteAccountLoading: false,
1078 deleteAccountShowConfirm: false,
1080 this.context.router.history.push('/');
1081 } else if (res.op == UserOperation.GetSite) {
1082 const data = res.data as GetSiteResponse;
1083 this.state.siteRes = data;
1084 this.setState(this.state);
1085 } else if (res.op == UserOperation.AddAdmin) {
1086 const data = res.data as AddAdminResponse;
1087 this.state.siteRes.admins = data.admins;
1088 this.setState(this.state);