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} />
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}
619 this.handleUserSettingsNewPasswordChange
624 <div class="form-group row">
626 class="col-lg-5 col-form-label"
627 htmlFor="user-verify-password"
629 {i18n.t('verify_password')}
631 <div class="col-lg-7">
634 id="user-verify-password"
636 value={this.state.userSettingsForm.new_password_verify}
639 this.handleUserSettingsNewPasswordVerifyChange
644 <div class="form-group row">
646 class="col-lg-5 col-form-label"
647 htmlFor="user-old-password"
649 {i18n.t('old_password')}
651 <div class="col-lg-7">
654 id="user-old-password"
656 value={this.state.userSettingsForm.old_password}
659 this.handleUserSettingsOldPasswordChange
664 {WebSocketService.Instance.site.enable_nsfw && (
665 <div class="form-group">
666 <div class="form-check">
668 class="form-check-input"
671 checked={this.state.userSettingsForm.show_nsfw}
674 this.handleUserSettingsShowNsfwChange
677 <label class="form-check-label" htmlFor="user-show-nsfw">
678 {i18n.t('show_nsfw')}
683 <div class="form-group">
684 <div class="form-check">
686 class="form-check-input"
687 id="user-show-avatars"
689 checked={this.state.userSettingsForm.show_avatars}
692 this.handleUserSettingsShowAvatarsChange
695 <label class="form-check-label" htmlFor="user-show-avatars">
696 {i18n.t('show_avatars')}
700 <div class="form-group">
701 <div class="form-check">
703 class="form-check-input"
704 id="user-send-notifications-to-email"
706 disabled={!this.state.user.email}
708 this.state.userSettingsForm.send_notifications_to_email
712 this.handleUserSettingsSendNotificationsToEmailChange
716 class="form-check-label"
717 htmlFor="user-send-notifications-to-email"
719 {i18n.t('send_notifications_to_email')}
723 <div class="form-group">
724 <button type="submit" class="btn btn-block btn-secondary mr-4">
725 {this.state.userSettingsLoading ? (
726 <svg class="icon icon-spinner spin">
727 <use xlinkHref="#icon-spinner"></use>
730 capitalizeFirstLetter(i18n.t('save'))
735 <div class="form-group mb-0">
737 class="btn btn-block btn-danger"
740 this.handleDeleteAccountShowConfirmToggle
743 {i18n.t('delete_account')}
745 {this.state.deleteAccountShowConfirm && (
747 <div class="my-2 alert alert-danger" role="alert">
748 {i18n.t('delete_account_confirm')}
752 value={this.state.deleteAccountForm.password}
755 this.handleDeleteAccountPasswordChange
757 class="form-control my-2"
760 class="btn btn-danger mr-4"
761 disabled={!this.state.deleteAccountForm.password}
762 onClick={linkEvent(this, this.handleDeleteAccount)}
764 {this.state.deleteAccountLoading ? (
765 <svg class="icon icon-spinner spin">
766 <use xlinkHref="#icon-spinner"></use>
769 capitalizeFirstLetter(i18n.t('delete'))
773 class="btn btn-secondary"
776 this.handleDeleteAccountShowConfirmToggle
794 {this.state.moderates.length > 0 && (
795 <div class="card border-secondary mb-3">
796 <div class="card-body">
797 <h5>{i18n.t('moderates')}</h5>
798 <ul class="list-unstyled mb-0">
799 {this.state.moderates.map(community => (
801 <Link to={`/c/${community.community_name}`}>
802 {community.community_name}
817 {this.state.follows.length > 0 && (
818 <div class="card border-secondary mb-3">
819 <div class="card-body">
820 <h5>{i18n.t('subscribed')}</h5>
821 <ul class="list-unstyled mb-0">
822 {this.state.follows.map(community => (
824 <Link to={`/c/${community.community_name}`}>
825 {community.community_name}
840 {this.state.page > 1 && (
842 class="btn btn-sm btn-secondary mr-1"
843 onClick={linkEvent(this, this.prevPage)}
849 class="btn btn-sm btn-secondary"
850 onClick={linkEvent(this, this.nextPage)}
859 let viewStr = View[this.state.view].toLowerCase();
860 let sortStr = SortType[this.state.sort].toLowerCase();
861 this.props.history.push(
862 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
881 let form: GetUserDetailsForm = {
882 user_id: this.state.user_id,
883 username: this.state.username,
884 sort: SortType[this.state.sort],
885 saved_only: this.state.view == View.Saved,
886 page: this.state.page,
889 WebSocketService.Instance.getUserDetails(form);
892 handleSortChange(val: SortType) {
893 this.state.sort = val;
895 this.setState(this.state);
900 handleViewChange(i: User, event: any) {
901 i.state.view = Number(event.target.value);
908 handleUserSettingsShowNsfwChange(i: User, event: any) {
909 i.state.userSettingsForm.show_nsfw = event.target.checked;
913 handleUserSettingsShowAvatarsChange(i: User, event: any) {
914 i.state.userSettingsForm.show_avatars = event.target.checked;
915 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
919 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
920 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
924 handleUserSettingsThemeChange(i: User, event: any) {
925 i.state.userSettingsForm.theme = event.target.value;
926 setTheme(event.target.value);
930 handleUserSettingsLangChange(i: User, event: any) {
931 i.state.userSettingsForm.lang = event.target.value;
932 i18n.changeLanguage(i.state.userSettingsForm.lang);
936 handleUserSettingsSortTypeChange(val: SortType) {
937 this.state.userSettingsForm.default_sort_type = val;
938 this.setState(this.state);
941 handleUserSettingsListingTypeChange(val: ListingType) {
942 this.state.userSettingsForm.default_listing_type = val;
943 this.setState(this.state);
946 handleUserSettingsEmailChange(i: User, event: any) {
947 i.state.userSettingsForm.email = event.target.value;
948 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
949 i.state.userSettingsForm.email = undefined;
954 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
955 i.state.userSettingsForm.matrix_user_id = event.target.value;
957 i.state.userSettingsForm.matrix_user_id == '' &&
958 !i.state.user.matrix_user_id
960 i.state.userSettingsForm.matrix_user_id = undefined;
965 handleUserSettingsNewPasswordChange(i: User, event: any) {
966 i.state.userSettingsForm.new_password = event.target.value;
967 if (i.state.userSettingsForm.new_password == '') {
968 i.state.userSettingsForm.new_password = undefined;
973 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
974 i.state.userSettingsForm.new_password_verify = event.target.value;
975 if (i.state.userSettingsForm.new_password_verify == '') {
976 i.state.userSettingsForm.new_password_verify = undefined;
981 handleUserSettingsOldPasswordChange(i: User, event: any) {
982 i.state.userSettingsForm.old_password = event.target.value;
983 if (i.state.userSettingsForm.old_password == '') {
984 i.state.userSettingsForm.old_password = undefined;
989 handleImageUpload(i: User, event: any) {
990 event.preventDefault();
991 let file = event.target.files[0];
992 const imageUploadUrl = `/pictshare/api/upload.php`;
993 const formData = new FormData();
994 formData.append('file', file);
996 i.state.avatarLoading = true;
999 fetch(imageUploadUrl, {
1003 .then(res => res.json())
1005 let url = `${window.location.origin}/pictshare/${res.url}`;
1006 if (res.filetype == 'mp4') {
1009 i.state.userSettingsForm.avatar = url;
1011 i.state.avatarLoading = false;
1012 i.setState(i.state);
1015 i.state.avatarLoading = false;
1016 i.setState(i.state);
1017 toast(error, 'danger');
1021 handleUserSettingsSubmit(i: User, event: any) {
1022 event.preventDefault();
1023 i.state.userSettingsLoading = true;
1024 i.setState(i.state);
1026 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1029 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1030 event.preventDefault();
1031 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1032 i.setState(i.state);
1035 handleDeleteAccountPasswordChange(i: User, event: any) {
1036 i.state.deleteAccountForm.password = event.target.value;
1037 i.setState(i.state);
1040 handleLogoutClick(i: User) {
1041 UserService.Instance.logout();
1042 i.context.router.history.push('/');
1045 handleDeleteAccount(i: User, event: any) {
1046 event.preventDefault();
1047 i.state.deleteAccountLoading = true;
1048 i.setState(i.state);
1050 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1053 parseMessage(msg: WebSocketJsonResponse) {
1055 let res = wsJsonToRes(msg);
1057 toast(i18n.t(msg.error), 'danger');
1058 this.state.deleteAccountLoading = false;
1059 this.state.avatarLoading = false;
1060 this.state.userSettingsLoading = false;
1061 if (msg.error == 'couldnt_find_that_username_or_email') {
1062 this.context.router.history.push('/');
1064 this.setState(this.state);
1066 } else if (msg.reconnect) {
1068 } else if (res.op == UserOperation.GetUserDetails) {
1069 let data = res.data as UserDetailsResponse;
1070 this.state.user = data.user;
1071 this.state.comments = data.comments;
1072 this.state.follows = data.follows;
1073 this.state.moderates = data.moderates;
1074 this.state.posts = data.posts;
1075 this.state.admins = data.admins;
1076 this.state.loading = false;
1077 if (this.isCurrentUser) {
1078 this.state.userSettingsForm.show_nsfw =
1079 UserService.Instance.user.show_nsfw;
1080 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1081 ? UserService.Instance.user.theme
1083 this.state.userSettingsForm.default_sort_type =
1084 UserService.Instance.user.default_sort_type;
1085 this.state.userSettingsForm.default_listing_type =
1086 UserService.Instance.user.default_listing_type;
1087 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1088 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1089 this.state.userSettingsForm.email = this.state.user.email;
1090 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1091 this.state.userSettingsForm.show_avatars =
1092 UserService.Instance.user.show_avatars;
1093 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1095 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1096 window.scrollTo(0, 0);
1097 this.setState(this.state);
1099 } else if (res.op == UserOperation.EditComment) {
1100 let data = res.data as CommentResponse;
1101 editCommentRes(data, this.state.comments);
1102 this.setState(this.state);
1103 } else if (res.op == UserOperation.CreateComment) {
1104 let data = res.data as CommentResponse;
1106 UserService.Instance.user &&
1107 data.comment.creator_id == UserService.Instance.user.id
1109 toast(i18n.t('reply_sent'));
1111 } else if (res.op == UserOperation.SaveComment) {
1112 let data = res.data as CommentResponse;
1113 saveCommentRes(data, this.state.comments);
1114 this.setState(this.state);
1115 } else if (res.op == UserOperation.CreateCommentLike) {
1116 let data = res.data as CommentResponse;
1117 createCommentLikeRes(data, this.state.comments);
1118 this.setState(this.state);
1119 } else if (res.op == UserOperation.CreatePostLike) {
1120 let data = res.data as PostResponse;
1121 createPostLikeFindRes(data, this.state.posts);
1122 this.setState(this.state);
1123 } else if (res.op == UserOperation.BanUser) {
1124 let data = res.data as BanUserResponse;
1126 .filter(c => c.creator_id == data.user.id)
1127 .forEach(c => (c.banned = data.banned));
1129 .filter(c => c.creator_id == data.user.id)
1130 .forEach(c => (c.banned = data.banned));
1131 this.setState(this.state);
1132 } else if (res.op == UserOperation.AddAdmin) {
1133 let data = res.data as AddAdminResponse;
1134 this.state.admins = data.admins;
1135 this.setState(this.state);
1136 } else if (res.op == UserOperation.SaveUserSettings) {
1137 let data = res.data as LoginResponse;
1138 this.state = this.emptyState;
1139 this.state.userSettingsLoading = false;
1140 this.setState(this.state);
1141 UserService.Instance.login(data);
1142 } else if (res.op == UserOperation.DeleteAccount) {
1143 this.state.deleteAccountLoading = false;
1144 this.state.deleteAccountShowConfirm = false;
1145 this.setState(this.state);
1146 this.context.router.history.push('/');