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]
322 <svg class="icon mx-2 text-muted small">
323 <use xlinkHref="#icon-rss">#</use>
331 let combined: Array<{ type_: string; data: Comment | Post }> = [];
332 let comments = this.state.comments.map(e => {
333 return { type_: 'comments', data: e };
335 let posts = this.state.posts.map(e => {
336 return { type_: 'posts', data: e };
339 combined.push(...comments);
340 combined.push(...posts);
343 if (this.state.sort == SortType.New) {
344 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
346 combined.sort((a, b) => b.data.score - a.data.score);
353 {i.type_ == 'posts' ? (
355 post={i.data as Post}
356 admins={this.state.admins}
361 nodes={[{ comment: i.data as Comment }]}
362 admins={this.state.admins}
377 nodes={commentsToFlatNodes(this.state.comments)}
378 admins={this.state.admins}
389 {this.state.posts.map(post => (
390 <PostListing post={post} admins={this.state.admins} showCommunity />
397 let user = this.state.user;
400 <div class="card border-secondary mb-3">
401 <div class="card-body">
403 <ul class="list-inline mb-0">
404 <li className="list-inline-item">
405 <UserListing user={user} realLink />
408 <li className="list-inline-item badge badge-danger">
415 {i18n.t('joined')} <MomentTime data={user} showAgo />
417 <div class="table-responsive mt-1">
418 <table class="table table-bordered table-sm mt-2 mb-0">
421 <td class="text-center" colSpan={2}>
422 {i18n.t('number_of_points', {
423 count: user.post_score + user.comment_score,
431 {i18n.t('number_of_points', { count: user.post_score })}
435 {i18n.t('number_of_posts', { count: user.number_of_posts })}
441 {i18n.t('number_of_points', { count: user.comment_score })}
445 {i18n.t('number_of_comments', {
446 count: user.number_of_comments,
452 {this.isCurrentUser ? (
454 class="btn btn-block btn-secondary mt-3"
455 onClick={linkEvent(this, this.handleLogoutClick)}
462 className={`btn btn-block btn-secondary mt-3 ${
463 !this.state.user.matrix_user_id && 'disabled'
466 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
468 {i18n.t('send_secure_message')}
471 class="btn btn-block btn-secondary mt-3"
472 to={`/create_private_message?recipient_id=${this.state.user.id}`}
474 {i18n.t('send_message')}
487 <div class="card border-secondary mb-3">
488 <div class="card-body">
489 <h5>{i18n.t('settings')}</h5>
490 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
491 <div class="form-group">
492 <label>{i18n.t('avatar')}</label>
493 <form class="d-inline">
495 htmlFor="file-upload"
496 class="pointer ml-4 text-muted small font-weight-bold"
498 {!this.state.userSettingsForm.avatar ? (
499 <span class="btn btn-sm btn-secondary">
500 {i18n.t('upload_avatar')}
506 src={this.state.userSettingsForm.avatar}
507 class="rounded-circle"
514 accept="image/*,video/*"
517 disabled={!UserService.Instance.user}
518 onChange={linkEvent(this, this.handleImageUpload)}
522 <div class="form-group">
523 <label>{i18n.t('language')}</label>
525 value={this.state.userSettingsForm.lang}
526 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
527 class="ml-2 custom-select custom-select-sm w-auto"
529 <option disabled>{i18n.t('language')}</option>
530 <option value="browser">{i18n.t('browser_default')}</option>
531 <option disabled>──</option>
532 {languages.map(lang => (
533 <option value={lang.code}>{lang.name}</option>
537 <div class="form-group">
538 <label>{i18n.t('theme')}</label>
540 value={this.state.userSettingsForm.theme}
541 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
542 class="ml-2 custom-select custom-select-sm w-auto"
544 <option disabled>{i18n.t('theme')}</option>
545 {themes.map(theme => (
546 <option value={theme}>{theme}</option>
550 <form className="form-group">
552 <div class="mr-2">{i18n.t('sort_type')}</div>
555 type_={this.state.userSettingsForm.default_listing_type}
556 onChange={this.handleUserSettingsListingTypeChange}
559 <form className="form-group">
561 <div class="mr-2">{i18n.t('type')}</div>
564 sort={this.state.userSettingsForm.default_sort_type}
565 onChange={this.handleUserSettingsSortTypeChange}
568 <div class="form-group row">
569 <label class="col-lg-3 col-form-label" htmlFor="user-email">
572 <div class="col-lg-9">
577 placeholder={i18n.t('optional')}
578 value={this.state.userSettingsForm.email}
581 this.handleUserSettingsEmailChange
587 <div class="form-group row">
588 <label class="col-lg-5 col-form-label">
589 <a href="https://about.riot.im/" target="_blank">
590 {i18n.t('matrix_user_id')}
593 <div class="col-lg-7">
597 placeholder="@user:example.com"
598 value={this.state.userSettingsForm.matrix_user_id}
601 this.handleUserSettingsMatrixUserIdChange
607 <div class="form-group row">
608 <label class="col-lg-5 col-form-label" htmlFor="user-password">
609 {i18n.t('new_password')}
611 <div class="col-lg-7">
616 value={this.state.userSettingsForm.new_password}
617 autoComplete="new-password"
620 this.handleUserSettingsNewPasswordChange
625 <div class="form-group row">
627 class="col-lg-5 col-form-label"
628 htmlFor="user-verify-password"
630 {i18n.t('verify_password')}
632 <div class="col-lg-7">
635 id="user-verify-password"
637 value={this.state.userSettingsForm.new_password_verify}
638 autoComplete="new-password"
641 this.handleUserSettingsNewPasswordVerifyChange
646 <div class="form-group row">
648 class="col-lg-5 col-form-label"
649 htmlFor="user-old-password"
651 {i18n.t('old_password')}
653 <div class="col-lg-7">
656 id="user-old-password"
658 value={this.state.userSettingsForm.old_password}
659 autoComplete="new-password"
662 this.handleUserSettingsOldPasswordChange
667 {WebSocketService.Instance.site.enable_nsfw && (
668 <div class="form-group">
669 <div class="form-check">
671 class="form-check-input"
674 checked={this.state.userSettingsForm.show_nsfw}
677 this.handleUserSettingsShowNsfwChange
680 <label class="form-check-label" htmlFor="user-show-nsfw">
681 {i18n.t('show_nsfw')}
686 <div class="form-group">
687 <div class="form-check">
689 class="form-check-input"
690 id="user-show-avatars"
692 checked={this.state.userSettingsForm.show_avatars}
695 this.handleUserSettingsShowAvatarsChange
698 <label class="form-check-label" htmlFor="user-show-avatars">
699 {i18n.t('show_avatars')}
703 <div class="form-group">
704 <div class="form-check">
706 class="form-check-input"
707 id="user-send-notifications-to-email"
709 disabled={!this.state.user.email}
711 this.state.userSettingsForm.send_notifications_to_email
715 this.handleUserSettingsSendNotificationsToEmailChange
719 class="form-check-label"
720 htmlFor="user-send-notifications-to-email"
722 {i18n.t('send_notifications_to_email')}
726 <div class="form-group">
727 <button type="submit" class="btn btn-block btn-secondary mr-4">
728 {this.state.userSettingsLoading ? (
729 <svg class="icon icon-spinner spin">
730 <use xlinkHref="#icon-spinner"></use>
733 capitalizeFirstLetter(i18n.t('save'))
738 <div class="form-group mb-0">
740 class="btn btn-block btn-danger"
743 this.handleDeleteAccountShowConfirmToggle
746 {i18n.t('delete_account')}
748 {this.state.deleteAccountShowConfirm && (
750 <div class="my-2 alert alert-danger" role="alert">
751 {i18n.t('delete_account_confirm')}
755 value={this.state.deleteAccountForm.password}
756 autoComplete="new-password"
759 this.handleDeleteAccountPasswordChange
761 class="form-control my-2"
764 class="btn btn-danger mr-4"
765 disabled={!this.state.deleteAccountForm.password}
766 onClick={linkEvent(this, this.handleDeleteAccount)}
768 {this.state.deleteAccountLoading ? (
769 <svg class="icon icon-spinner spin">
770 <use xlinkHref="#icon-spinner"></use>
773 capitalizeFirstLetter(i18n.t('delete'))
777 class="btn btn-secondary"
780 this.handleDeleteAccountShowConfirmToggle
798 {this.state.moderates.length > 0 && (
799 <div class="card border-secondary mb-3">
800 <div class="card-body">
801 <h5>{i18n.t('moderates')}</h5>
802 <ul class="list-unstyled mb-0">
803 {this.state.moderates.map(community => (
805 <Link to={`/c/${community.community_name}`}>
806 {community.community_name}
821 {this.state.follows.length > 0 && (
822 <div class="card border-secondary mb-3">
823 <div class="card-body">
824 <h5>{i18n.t('subscribed')}</h5>
825 <ul class="list-unstyled mb-0">
826 {this.state.follows.map(community => (
828 <Link to={`/c/${community.community_name}`}>
829 {community.community_name}
844 {this.state.page > 1 && (
846 class="btn btn-sm btn-secondary mr-1"
847 onClick={linkEvent(this, this.prevPage)}
853 class="btn btn-sm btn-secondary"
854 onClick={linkEvent(this, this.nextPage)}
863 let viewStr = View[this.state.view].toLowerCase();
864 let sortStr = SortType[this.state.sort].toLowerCase();
865 this.props.history.push(
866 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
885 let form: GetUserDetailsForm = {
886 user_id: this.state.user_id,
887 username: this.state.username,
888 sort: SortType[this.state.sort],
889 saved_only: this.state.view == View.Saved,
890 page: this.state.page,
893 WebSocketService.Instance.getUserDetails(form);
896 handleSortChange(val: SortType) {
897 this.state.sort = val;
899 this.setState(this.state);
904 handleViewChange(i: User, event: any) {
905 i.state.view = Number(event.target.value);
912 handleUserSettingsShowNsfwChange(i: User, event: any) {
913 i.state.userSettingsForm.show_nsfw = event.target.checked;
917 handleUserSettingsShowAvatarsChange(i: User, event: any) {
918 i.state.userSettingsForm.show_avatars = event.target.checked;
919 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
923 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
924 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
928 handleUserSettingsThemeChange(i: User, event: any) {
929 i.state.userSettingsForm.theme = event.target.value;
930 setTheme(event.target.value, true);
934 handleUserSettingsLangChange(i: User, event: any) {
935 i.state.userSettingsForm.lang = event.target.value;
936 i18n.changeLanguage(i.state.userSettingsForm.lang);
940 handleUserSettingsSortTypeChange(val: SortType) {
941 this.state.userSettingsForm.default_sort_type = val;
942 this.setState(this.state);
945 handleUserSettingsListingTypeChange(val: ListingType) {
946 this.state.userSettingsForm.default_listing_type = val;
947 this.setState(this.state);
950 handleUserSettingsEmailChange(i: User, event: any) {
951 i.state.userSettingsForm.email = event.target.value;
952 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
953 i.state.userSettingsForm.email = undefined;
958 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
959 i.state.userSettingsForm.matrix_user_id = event.target.value;
961 i.state.userSettingsForm.matrix_user_id == '' &&
962 !i.state.user.matrix_user_id
964 i.state.userSettingsForm.matrix_user_id = undefined;
969 handleUserSettingsNewPasswordChange(i: User, event: any) {
970 i.state.userSettingsForm.new_password = event.target.value;
971 if (i.state.userSettingsForm.new_password == '') {
972 i.state.userSettingsForm.new_password = undefined;
977 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
978 i.state.userSettingsForm.new_password_verify = event.target.value;
979 if (i.state.userSettingsForm.new_password_verify == '') {
980 i.state.userSettingsForm.new_password_verify = undefined;
985 handleUserSettingsOldPasswordChange(i: User, event: any) {
986 i.state.userSettingsForm.old_password = event.target.value;
987 if (i.state.userSettingsForm.old_password == '') {
988 i.state.userSettingsForm.old_password = undefined;
993 handleImageUpload(i: User, event: any) {
994 event.preventDefault();
995 let file = event.target.files[0];
996 const imageUploadUrl = `/pictrs/image`;
997 const formData = new FormData();
998 formData.append('images[]', file);
1000 i.state.avatarLoading = true;
1001 i.setState(i.state);
1003 fetch(imageUploadUrl, {
1007 .then(res => res.json())
1009 console.log('pictrs upload:');
1011 if (res.msg == 'ok') {
1012 let hash = res.files[0].file;
1013 let url = `${window.location.origin}/pictrs/image/${hash}`;
1014 i.state.userSettingsForm.avatar = url;
1015 i.state.avatarLoading = false;
1016 i.setState(i.state);
1018 i.state.avatarLoading = false;
1019 i.setState(i.state);
1020 toast(JSON.stringify(res), 'danger');
1024 i.state.avatarLoading = false;
1025 i.setState(i.state);
1026 toast(error, 'danger');
1030 handleUserSettingsSubmit(i: User, event: any) {
1031 event.preventDefault();
1032 i.state.userSettingsLoading = true;
1033 i.setState(i.state);
1035 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1038 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1039 event.preventDefault();
1040 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1041 i.setState(i.state);
1044 handleDeleteAccountPasswordChange(i: User, event: any) {
1045 i.state.deleteAccountForm.password = event.target.value;
1046 i.setState(i.state);
1049 handleLogoutClick(i: User) {
1050 UserService.Instance.logout();
1051 i.context.router.history.push('/');
1054 handleDeleteAccount(i: User, event: any) {
1055 event.preventDefault();
1056 i.state.deleteAccountLoading = true;
1057 i.setState(i.state);
1059 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1062 parseMessage(msg: WebSocketJsonResponse) {
1064 let res = wsJsonToRes(msg);
1066 toast(i18n.t(msg.error), 'danger');
1067 this.state.deleteAccountLoading = false;
1068 this.state.avatarLoading = false;
1069 this.state.userSettingsLoading = false;
1070 if (msg.error == 'couldnt_find_that_username_or_email') {
1071 this.context.router.history.push('/');
1073 this.setState(this.state);
1075 } else if (msg.reconnect) {
1077 } else if (res.op == UserOperation.GetUserDetails) {
1078 let data = res.data as UserDetailsResponse;
1079 this.state.user = data.user;
1080 this.state.comments = data.comments;
1081 this.state.follows = data.follows;
1082 this.state.moderates = data.moderates;
1083 this.state.posts = data.posts;
1084 this.state.admins = data.admins;
1085 this.state.loading = false;
1086 if (this.isCurrentUser) {
1087 this.state.userSettingsForm.show_nsfw =
1088 UserService.Instance.user.show_nsfw;
1089 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1090 ? UserService.Instance.user.theme
1092 this.state.userSettingsForm.default_sort_type =
1093 UserService.Instance.user.default_sort_type;
1094 this.state.userSettingsForm.default_listing_type =
1095 UserService.Instance.user.default_listing_type;
1096 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1097 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1098 this.state.userSettingsForm.email = this.state.user.email;
1099 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1100 this.state.userSettingsForm.show_avatars =
1101 UserService.Instance.user.show_avatars;
1102 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1104 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1105 window.scrollTo(0, 0);
1106 this.setState(this.state);
1108 } else if (res.op == UserOperation.EditComment) {
1109 let data = res.data as CommentResponse;
1110 editCommentRes(data, this.state.comments);
1111 this.setState(this.state);
1112 } else if (res.op == UserOperation.CreateComment) {
1113 let data = res.data as CommentResponse;
1115 UserService.Instance.user &&
1116 data.comment.creator_id == UserService.Instance.user.id
1118 toast(i18n.t('reply_sent'));
1120 } else if (res.op == UserOperation.SaveComment) {
1121 let data = res.data as CommentResponse;
1122 saveCommentRes(data, this.state.comments);
1123 this.setState(this.state);
1124 } else if (res.op == UserOperation.CreateCommentLike) {
1125 let data = res.data as CommentResponse;
1126 createCommentLikeRes(data, this.state.comments);
1127 this.setState(this.state);
1128 } else if (res.op == UserOperation.CreatePostLike) {
1129 let data = res.data as PostResponse;
1130 createPostLikeFindRes(data, this.state.posts);
1131 this.setState(this.state);
1132 } else if (res.op == UserOperation.BanUser) {
1133 let data = res.data as BanUserResponse;
1135 .filter(c => c.creator_id == data.user.id)
1136 .forEach(c => (c.banned = data.banned));
1138 .filter(c => c.creator_id == data.user.id)
1139 .forEach(c => (c.banned = data.banned));
1140 this.setState(this.state);
1141 } else if (res.op == UserOperation.AddAdmin) {
1142 let data = res.data as AddAdminResponse;
1143 this.state.admins = data.admins;
1144 this.setState(this.state);
1145 } else if (res.op == UserOperation.SaveUserSettings) {
1146 let data = res.data as LoginResponse;
1147 this.state = this.emptyState;
1148 this.state.userSettingsLoading = false;
1149 this.setState(this.state);
1150 UserService.Instance.login(data);
1151 } else if (res.op == UserOperation.DeleteAccount) {
1152 this.state.deleteAccountLoading = false;
1153 this.state.deleteAccountShowConfirm = false;
1154 this.setState(this.state);
1155 this.context.router.history.push('/');