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';
22 WebSocketJsonResponse,
23 } from '../interfaces';
24 import { WebSocketService, UserService } from '../services';
29 capitalizeFirstLetter,
38 createPostLikeFindRes,
42 import { PostListing } from './post-listing';
43 import { UserListing } from './user-listing';
44 import { SortSelect } from './sort-select';
45 import { ListingTypeSelect } from './listing-type-select';
46 import { CommentNodes } from './comment-nodes';
47 import { MomentTime } from './moment-time';
48 import { i18n } from '../i18next';
61 follows: Array<CommunityUser>;
62 moderates: Array<CommunityUser>;
63 comments: Array<Comment>;
66 admins: Array<UserView>;
71 avatarLoading: boolean;
72 userSettingsForm: UserSettingsForm;
73 userSettingsLoading: boolean;
74 deleteAccountLoading: boolean;
75 deleteAccountShowConfirm: boolean;
76 deleteAccountForm: DeleteAccountForm;
79 export class User extends Component<any, UserState> {
80 private subscription: Subscription;
81 private emptyState: UserState = {
86 number_of_posts: null,
88 number_of_comments: null,
93 send_notifications_to_email: null,
105 avatarLoading: false,
106 view: this.getViewFromProps(this.props),
107 sort: this.getSortTypeFromProps(this.props),
108 page: this.getPageFromProps(this.props),
112 default_sort_type: null,
113 default_listing_type: null,
116 send_notifications_to_email: null,
119 userSettingsLoading: null,
120 deleteAccountLoading: null,
121 deleteAccountShowConfirm: false,
127 constructor(props: any, context: any) {
128 super(props, context);
130 this.state = this.emptyState;
131 this.handleSortChange = this.handleSortChange.bind(this);
132 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
135 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
139 this.state.user_id = Number(this.props.match.params.id);
140 this.state.username = this.props.match.params.username;
142 this.subscription = WebSocketService.Instance.subject
143 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
145 msg => this.parseMessage(msg),
146 err => console.error(err),
147 () => console.log('complete')
153 get isCurrentUser() {
155 UserService.Instance.user &&
156 UserService.Instance.user.id == this.state.user.id
160 getViewFromProps(props: any): View {
161 return props.match.params.view
162 ? View[capitalizeFirstLetter(props.match.params.view)]
166 getSortTypeFromProps(props: any): SortType {
167 return props.match.params.sort
168 ? routeSortTypeToEnum(props.match.params.sort)
172 getPageFromProps(props: any): number {
173 return props.match.params.page ? Number(props.match.params.page) : 1;
176 componentWillUnmount() {
177 this.subscription.unsubscribe();
180 // Necessary for back button for some reason
181 componentWillReceiveProps(nextProps: any) {
183 nextProps.history.action == 'POP' ||
184 nextProps.history.action == 'PUSH'
186 this.state.view = this.getViewFromProps(nextProps);
187 this.state.sort = this.getSortTypeFromProps(nextProps);
188 this.state.page = this.getPageFromProps(nextProps);
189 this.setState(this.state);
194 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
195 // Necessary if you are on a post and you click another post (same route)
197 lastProps.location.pathname.split('/')[2] !==
198 lastProps.history.location.pathname.split('/')[2]
200 // Couldnt get a refresh working. This does for now.
207 <div class="container">
208 {this.state.loading ? (
210 <svg class="icon icon-spinner spin">
211 <use xlinkHref="#icon-spinner"></use>
216 <div class="col-12 col-md-8">
218 {this.state.user.avatar && showAvatars() && (
222 src={this.state.user.avatar}
223 class="rounded-circle mr-2"
226 <span>/u/{this.state.user.name}</span>
229 {this.state.view == View.Overview && this.overview()}
230 {this.state.view == View.Comments && this.comments()}
231 {this.state.view == View.Posts && this.posts()}
232 {this.state.view == View.Saved && this.overview()}
235 <div class="col-12 col-md-4">
237 {this.isCurrentUser && this.userSettings()}
249 <div class="btn-group btn-group-toggle">
251 className={`btn btn-sm btn-secondary pointer btn-outline-light
252 ${this.state.view == View.Overview && 'active'}
257 value={View.Overview}
258 checked={this.state.view == View.Overview}
259 onChange={linkEvent(this, this.handleViewChange)}
264 className={`btn btn-sm btn-secondary pointer btn-outline-light
265 ${this.state.view == View.Comments && 'active'}
270 value={View.Comments}
271 checked={this.state.view == View.Comments}
272 onChange={linkEvent(this, this.handleViewChange)}
277 className={`btn btn-sm btn-secondary pointer btn-outline-light
278 ${this.state.view == View.Posts && 'active'}
284 checked={this.state.view == View.Posts}
285 onChange={linkEvent(this, this.handleViewChange)}
290 className={`btn btn-sm btn-secondary pointer btn-outline-light
291 ${this.state.view == View.Saved && 'active'}
297 checked={this.state.view == View.Saved}
298 onChange={linkEvent(this, this.handleViewChange)}
308 <div className="mb-2">
309 <span class="mr-3">{this.viewRadios()}</span>
311 sort={this.state.sort}
312 onChange={this.handleSortChange}
316 href={`/feeds/u/${this.state.username}.xml?sort=${
317 SortType[this.state.sort]
323 <svg class="icon mx-2 text-muted small">
324 <use xlinkHref="#icon-rss">#</use>
332 let combined: Array<{ type_: string; data: Comment | Post }> = [];
333 let comments = this.state.comments.map(e => {
334 return { type_: 'comments', data: e };
336 let posts = this.state.posts.map(e => {
337 return { type_: 'posts', data: e };
340 combined.push(...comments);
341 combined.push(...posts);
344 if (this.state.sort == SortType.New) {
345 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
347 combined.sort((a, b) => b.data.score - a.data.score);
354 {i.type_ == 'posts' ? (
356 post={i.data as Post}
357 admins={this.state.admins}
362 nodes={[{ comment: i.data as Comment }]}
363 admins={this.state.admins}
378 nodes={commentsToFlatNodes(this.state.comments)}
379 admins={this.state.admins}
390 {this.state.posts.map(post => (
391 <PostListing post={post} admins={this.state.admins} showCommunity />
398 let user = this.state.user;
401 <div class="card border-secondary mb-3">
402 <div class="card-body">
404 <ul class="list-inline mb-0">
405 <li className="list-inline-item">
406 <UserListing user={user} realLink />
409 <li className="list-inline-item badge badge-danger">
416 {i18n.t('joined')} <MomentTime data={user} showAgo />
418 <div class="table-responsive mt-1">
419 <table class="table table-bordered table-sm mt-2 mb-0">
422 <td class="text-center" colSpan={2}>
423 {i18n.t('number_of_points', {
424 count: user.post_score + user.comment_score,
432 {i18n.t('number_of_points', { count: user.post_score })}
436 {i18n.t('number_of_posts', { count: user.number_of_posts })}
442 {i18n.t('number_of_points', { count: user.comment_score })}
446 {i18n.t('number_of_comments', {
447 count: user.number_of_comments,
453 {this.isCurrentUser ? (
455 class="btn btn-block btn-secondary mt-3"
456 onClick={linkEvent(this, this.handleLogoutClick)}
463 className={`btn btn-block btn-secondary mt-3 ${
464 !this.state.user.matrix_user_id && 'disabled'
468 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
470 {i18n.t('send_secure_message')}
473 class="btn btn-block btn-secondary mt-3"
474 to={`/create_private_message?recipient_id=${this.state.user.id}`}
476 {i18n.t('send_message')}
489 <div class="card border-secondary mb-3">
490 <div class="card-body">
491 <h5>{i18n.t('settings')}</h5>
492 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
493 <div class="form-group">
494 <label>{i18n.t('avatar')}</label>
495 <form class="d-inline">
497 htmlFor="file-upload"
498 class="pointer ml-4 text-muted small font-weight-bold"
500 {!this.state.userSettingsForm.avatar ? (
501 <span class="btn btn-sm btn-secondary">
502 {i18n.t('upload_avatar')}
508 src={this.state.userSettingsForm.avatar}
509 class="rounded-circle"
516 accept="image/*,video/*"
519 disabled={!UserService.Instance.user}
520 onChange={linkEvent(this, this.handleImageUpload)}
524 <div class="form-group">
525 <label>{i18n.t('language')}</label>
527 value={this.state.userSettingsForm.lang}
528 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
529 class="ml-2 custom-select custom-select-sm w-auto"
531 <option disabled>{i18n.t('language')}</option>
532 <option value="browser">{i18n.t('browser_default')}</option>
533 <option disabled>──</option>
534 {languages.map(lang => (
535 <option value={lang.code}>{lang.name}</option>
539 <div class="form-group">
540 <label>{i18n.t('theme')}</label>
542 value={this.state.userSettingsForm.theme}
543 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
544 class="ml-2 custom-select custom-select-sm w-auto"
546 <option disabled>{i18n.t('theme')}</option>
547 {themes.map(theme => (
548 <option value={theme}>{theme}</option>
552 <form className="form-group">
554 <div class="mr-2">{i18n.t('sort_type')}</div>
557 type_={this.state.userSettingsForm.default_listing_type}
558 onChange={this.handleUserSettingsListingTypeChange}
561 <form className="form-group">
563 <div class="mr-2">{i18n.t('type')}</div>
566 sort={this.state.userSettingsForm.default_sort_type}
567 onChange={this.handleUserSettingsSortTypeChange}
570 <div class="form-group row">
571 <label class="col-lg-3 col-form-label" htmlFor="user-email">
574 <div class="col-lg-9">
579 placeholder={i18n.t('optional')}
580 value={this.state.userSettingsForm.email}
583 this.handleUserSettingsEmailChange
589 <div class="form-group row">
590 <label class="col-lg-5 col-form-label">
592 href="https://about.riot.im/"
596 {i18n.t('matrix_user_id')}
599 <div class="col-lg-7">
603 placeholder="@user:example.com"
604 value={this.state.userSettingsForm.matrix_user_id}
607 this.handleUserSettingsMatrixUserIdChange
613 <div class="form-group row">
614 <label class="col-lg-5 col-form-label" htmlFor="user-password">
615 {i18n.t('new_password')}
617 <div class="col-lg-7">
622 value={this.state.userSettingsForm.new_password}
623 autoComplete="new-password"
626 this.handleUserSettingsNewPasswordChange
631 <div class="form-group row">
633 class="col-lg-5 col-form-label"
634 htmlFor="user-verify-password"
636 {i18n.t('verify_password')}
638 <div class="col-lg-7">
641 id="user-verify-password"
643 value={this.state.userSettingsForm.new_password_verify}
644 autoComplete="new-password"
647 this.handleUserSettingsNewPasswordVerifyChange
652 <div class="form-group row">
654 class="col-lg-5 col-form-label"
655 htmlFor="user-old-password"
657 {i18n.t('old_password')}
659 <div class="col-lg-7">
662 id="user-old-password"
664 value={this.state.userSettingsForm.old_password}
665 autoComplete="new-password"
668 this.handleUserSettingsOldPasswordChange
673 {WebSocketService.Instance.site.enable_nsfw && (
674 <div class="form-group">
675 <div class="form-check">
677 class="form-check-input"
680 checked={this.state.userSettingsForm.show_nsfw}
683 this.handleUserSettingsShowNsfwChange
686 <label class="form-check-label" htmlFor="user-show-nsfw">
687 {i18n.t('show_nsfw')}
692 <div class="form-group">
693 <div class="form-check">
695 class="form-check-input"
696 id="user-show-avatars"
698 checked={this.state.userSettingsForm.show_avatars}
701 this.handleUserSettingsShowAvatarsChange
704 <label class="form-check-label" htmlFor="user-show-avatars">
705 {i18n.t('show_avatars')}
709 <div class="form-group">
710 <div class="form-check">
712 class="form-check-input"
713 id="user-send-notifications-to-email"
715 disabled={!this.state.user.email}
717 this.state.userSettingsForm.send_notifications_to_email
721 this.handleUserSettingsSendNotificationsToEmailChange
725 class="form-check-label"
726 htmlFor="user-send-notifications-to-email"
728 {i18n.t('send_notifications_to_email')}
732 <div class="form-group">
733 <button type="submit" class="btn btn-block btn-secondary mr-4">
734 {this.state.userSettingsLoading ? (
735 <svg class="icon icon-spinner spin">
736 <use xlinkHref="#icon-spinner"></use>
739 capitalizeFirstLetter(i18n.t('save'))
744 <div class="form-group mb-0">
746 class="btn btn-block btn-danger"
749 this.handleDeleteAccountShowConfirmToggle
752 {i18n.t('delete_account')}
754 {this.state.deleteAccountShowConfirm && (
756 <div class="my-2 alert alert-danger" role="alert">
757 {i18n.t('delete_account_confirm')}
761 value={this.state.deleteAccountForm.password}
762 autoComplete="new-password"
765 this.handleDeleteAccountPasswordChange
767 class="form-control my-2"
770 class="btn btn-danger mr-4"
771 disabled={!this.state.deleteAccountForm.password}
772 onClick={linkEvent(this, this.handleDeleteAccount)}
774 {this.state.deleteAccountLoading ? (
775 <svg class="icon icon-spinner spin">
776 <use xlinkHref="#icon-spinner"></use>
779 capitalizeFirstLetter(i18n.t('delete'))
783 class="btn btn-secondary"
786 this.handleDeleteAccountShowConfirmToggle
804 {this.state.moderates.length > 0 && (
805 <div class="card border-secondary mb-3">
806 <div class="card-body">
807 <h5>{i18n.t('moderates')}</h5>
808 <ul class="list-unstyled mb-0">
809 {this.state.moderates.map(community => (
811 <Link to={`/c/${community.community_name}`}>
812 {community.community_name}
827 {this.state.follows.length > 0 && (
828 <div class="card border-secondary mb-3">
829 <div class="card-body">
830 <h5>{i18n.t('subscribed')}</h5>
831 <ul class="list-unstyled mb-0">
832 {this.state.follows.map(community => (
834 <Link to={`/c/${community.community_name}`}>
835 {community.community_name}
850 {this.state.page > 1 && (
852 class="btn btn-sm btn-secondary mr-1"
853 onClick={linkEvent(this, this.prevPage)}
859 class="btn btn-sm btn-secondary"
860 onClick={linkEvent(this, this.nextPage)}
869 let viewStr = View[this.state.view].toLowerCase();
870 let sortStr = SortType[this.state.sort].toLowerCase();
871 this.props.history.push(
872 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
891 let form: GetUserDetailsForm = {
892 user_id: this.state.user_id,
893 username: this.state.username,
894 sort: SortType[this.state.sort],
895 saved_only: this.state.view == View.Saved,
896 page: this.state.page,
899 WebSocketService.Instance.getUserDetails(form);
902 handleSortChange(val: SortType) {
903 this.state.sort = val;
905 this.setState(this.state);
910 handleViewChange(i: User, event: any) {
911 i.state.view = Number(event.target.value);
918 handleUserSettingsShowNsfwChange(i: User, event: any) {
919 i.state.userSettingsForm.show_nsfw = event.target.checked;
923 handleUserSettingsShowAvatarsChange(i: User, event: any) {
924 i.state.userSettingsForm.show_avatars = event.target.checked;
925 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
929 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
930 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
934 handleUserSettingsThemeChange(i: User, event: any) {
935 i.state.userSettingsForm.theme = event.target.value;
936 setTheme(event.target.value, true);
940 handleUserSettingsLangChange(i: User, event: any) {
941 i.state.userSettingsForm.lang = event.target.value;
942 i18n.changeLanguage(i.state.userSettingsForm.lang);
946 handleUserSettingsSortTypeChange(val: SortType) {
947 this.state.userSettingsForm.default_sort_type = val;
948 this.setState(this.state);
951 handleUserSettingsListingTypeChange(val: ListingType) {
952 this.state.userSettingsForm.default_listing_type = val;
953 this.setState(this.state);
956 handleUserSettingsEmailChange(i: User, event: any) {
957 i.state.userSettingsForm.email = event.target.value;
958 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
959 i.state.userSettingsForm.email = undefined;
964 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
965 i.state.userSettingsForm.matrix_user_id = event.target.value;
967 i.state.userSettingsForm.matrix_user_id == '' &&
968 !i.state.user.matrix_user_id
970 i.state.userSettingsForm.matrix_user_id = undefined;
975 handleUserSettingsNewPasswordChange(i: User, event: any) {
976 i.state.userSettingsForm.new_password = event.target.value;
977 if (i.state.userSettingsForm.new_password == '') {
978 i.state.userSettingsForm.new_password = undefined;
983 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
984 i.state.userSettingsForm.new_password_verify = event.target.value;
985 if (i.state.userSettingsForm.new_password_verify == '') {
986 i.state.userSettingsForm.new_password_verify = undefined;
991 handleUserSettingsOldPasswordChange(i: User, event: any) {
992 i.state.userSettingsForm.old_password = event.target.value;
993 if (i.state.userSettingsForm.old_password == '') {
994 i.state.userSettingsForm.old_password = undefined;
999 handleImageUpload(i: User, event: any) {
1000 event.preventDefault();
1001 let file = event.target.files[0];
1002 const imageUploadUrl = `/pictrs/image`;
1003 const formData = new FormData();
1004 formData.append('images[]', file);
1006 i.state.avatarLoading = true;
1007 i.setState(i.state);
1009 fetch(imageUploadUrl, {
1013 .then(res => res.json())
1015 console.log('pictrs upload:');
1017 if (res.msg == 'ok') {
1018 let hash = res.files[0].file;
1019 let url = `${window.location.origin}/pictrs/image/${hash}`;
1020 i.state.userSettingsForm.avatar = url;
1021 i.state.avatarLoading = false;
1022 i.setState(i.state);
1024 i.state.avatarLoading = false;
1025 i.setState(i.state);
1026 toast(JSON.stringify(res), 'danger');
1030 i.state.avatarLoading = false;
1031 i.setState(i.state);
1032 toast(error, 'danger');
1036 handleUserSettingsSubmit(i: User, event: any) {
1037 event.preventDefault();
1038 i.state.userSettingsLoading = true;
1039 i.setState(i.state);
1041 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1044 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1045 event.preventDefault();
1046 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1047 i.setState(i.state);
1050 handleDeleteAccountPasswordChange(i: User, event: any) {
1051 i.state.deleteAccountForm.password = event.target.value;
1052 i.setState(i.state);
1055 handleLogoutClick(i: User) {
1056 UserService.Instance.logout();
1057 i.context.router.history.push('/');
1060 handleDeleteAccount(i: User, event: any) {
1061 event.preventDefault();
1062 i.state.deleteAccountLoading = true;
1063 i.setState(i.state);
1065 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1068 parseMessage(msg: WebSocketJsonResponse) {
1070 let res = wsJsonToRes(msg);
1072 toast(i18n.t(msg.error), 'danger');
1073 this.state.deleteAccountLoading = false;
1074 this.state.avatarLoading = false;
1075 this.state.userSettingsLoading = false;
1076 if (msg.error == 'couldnt_find_that_username_or_email') {
1077 this.context.router.history.push('/');
1079 this.setState(this.state);
1081 } else if (msg.reconnect) {
1083 } else if (res.op == UserOperation.GetUserDetails) {
1084 let data = res.data as UserDetailsResponse;
1085 this.state.user = data.user;
1086 this.state.comments = data.comments;
1087 this.state.follows = data.follows;
1088 this.state.moderates = data.moderates;
1089 this.state.posts = data.posts;
1090 this.state.admins = data.admins;
1091 this.state.loading = false;
1092 if (this.isCurrentUser) {
1093 this.state.userSettingsForm.show_nsfw =
1094 UserService.Instance.user.show_nsfw;
1095 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1096 ? UserService.Instance.user.theme
1098 this.state.userSettingsForm.default_sort_type =
1099 UserService.Instance.user.default_sort_type;
1100 this.state.userSettingsForm.default_listing_type =
1101 UserService.Instance.user.default_listing_type;
1102 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1103 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1104 this.state.userSettingsForm.email = this.state.user.email;
1105 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1106 this.state.userSettingsForm.show_avatars =
1107 UserService.Instance.user.show_avatars;
1108 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1110 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1111 window.scrollTo(0, 0);
1112 this.setState(this.state);
1114 } else if (res.op == UserOperation.EditComment) {
1115 let data = res.data as CommentResponse;
1116 editCommentRes(data, this.state.comments);
1117 this.setState(this.state);
1118 } else if (res.op == UserOperation.CreateComment) {
1119 let data = res.data as CommentResponse;
1121 UserService.Instance.user &&
1122 data.comment.creator_id == UserService.Instance.user.id
1124 toast(i18n.t('reply_sent'));
1126 } else if (res.op == UserOperation.SaveComment) {
1127 let data = res.data as CommentResponse;
1128 saveCommentRes(data, this.state.comments);
1129 this.setState(this.state);
1130 } else if (res.op == UserOperation.CreateCommentLike) {
1131 let data = res.data as CommentResponse;
1132 createCommentLikeRes(data, this.state.comments);
1133 this.setState(this.state);
1134 } else if (res.op == UserOperation.CreatePostLike) {
1135 let data = res.data as PostResponse;
1136 createPostLikeFindRes(data, this.state.posts);
1137 this.setState(this.state);
1138 } else if (res.op == UserOperation.BanUser) {
1139 let data = res.data as BanUserResponse;
1141 .filter(c => c.creator_id == data.user.id)
1142 .forEach(c => (c.banned = data.banned));
1144 .filter(c => c.creator_id == data.user.id)
1145 .forEach(c => (c.banned = data.banned));
1146 this.setState(this.state);
1147 } else if (res.op == UserOperation.AddAdmin) {
1148 let data = res.data as AddAdminResponse;
1149 this.state.admins = data.admins;
1150 this.setState(this.state);
1151 } else if (res.op == UserOperation.SaveUserSettings) {
1152 let data = res.data as LoginResponse;
1153 this.state = this.emptyState;
1154 this.state.userSettingsLoading = false;
1155 this.setState(this.state);
1156 UserService.Instance.login(data);
1157 } else if (res.op == UserOperation.DeleteAccount) {
1158 this.state.deleteAccountLoading = false;
1159 this.state.deleteAccountShowConfirm = false;
1160 this.setState(this.state);
1161 this.context.router.history.push('/');