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 { SortSelect } from './sort-select';
44 import { ListingTypeSelect } from './listing-type-select';
45 import { CommentNodes } from './comment-nodes';
46 import { MomentTime } from './moment-time';
47 import { i18n } from '../i18next';
60 follows: Array<CommunityUser>;
61 moderates: Array<CommunityUser>;
62 comments: Array<Comment>;
65 admins: Array<UserView>;
70 avatarLoading: boolean;
71 userSettingsForm: UserSettingsForm;
72 userSettingsLoading: boolean;
73 deleteAccountLoading: boolean;
74 deleteAccountShowConfirm: boolean;
75 deleteAccountForm: DeleteAccountForm;
78 export class User extends Component<any, UserState> {
79 private subscription: Subscription;
80 private emptyState: UserState = {
86 number_of_posts: null,
88 number_of_comments: null,
93 send_notifications_to_email: null,
103 avatarLoading: false,
104 view: this.getViewFromProps(this.props),
105 sort: this.getSortTypeFromProps(this.props),
106 page: this.getPageFromProps(this.props),
110 default_sort_type: null,
111 default_listing_type: null,
114 send_notifications_to_email: null,
117 userSettingsLoading: null,
118 deleteAccountLoading: null,
119 deleteAccountShowConfirm: false,
125 constructor(props: any, context: any) {
126 super(props, context);
128 this.state = this.emptyState;
129 this.handleSortChange = this.handleSortChange.bind(this);
130 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
133 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
137 this.state.user_id = Number(this.props.match.params.id);
138 this.state.username = this.props.match.params.username;
140 this.subscription = WebSocketService.Instance.subject
141 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
143 msg => this.parseMessage(msg),
144 err => console.error(err),
145 () => console.log('complete')
151 get isCurrentUser() {
153 UserService.Instance.user &&
154 UserService.Instance.user.id == this.state.user.id
158 getViewFromProps(props: any): View {
159 return props.match.params.view
160 ? View[capitalizeFirstLetter(props.match.params.view)]
164 getSortTypeFromProps(props: any): SortType {
165 return props.match.params.sort
166 ? routeSortTypeToEnum(props.match.params.sort)
170 getPageFromProps(props: any): number {
171 return props.match.params.page ? Number(props.match.params.page) : 1;
174 componentWillUnmount() {
175 this.subscription.unsubscribe();
178 // Necessary for back button for some reason
179 componentWillReceiveProps(nextProps: any) {
181 nextProps.history.action == 'POP' ||
182 nextProps.history.action == 'PUSH'
184 this.state.view = this.getViewFromProps(nextProps);
185 this.state.sort = this.getSortTypeFromProps(nextProps);
186 this.state.page = this.getPageFromProps(nextProps);
187 this.setState(this.state);
192 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
193 // Necessary if you are on a post and you click another post (same route)
195 lastProps.location.pathname.split('/')[2] !==
196 lastProps.history.location.pathname.split('/')[2]
198 // Couldnt get a refresh working. This does for now.
205 <div class="container">
206 {this.state.loading ? (
208 <svg class="icon icon-spinner spin">
209 <use xlinkHref="#icon-spinner"></use>
214 <div class="col-12 col-md-8">
216 {this.state.user.avatar && showAvatars() && (
220 src={this.state.user.avatar}
221 class="rounded-circle mr-2"
224 <span>/u/{this.state.user.name}</span>
227 {this.state.view == View.Overview && this.overview()}
228 {this.state.view == View.Comments && this.comments()}
229 {this.state.view == View.Posts && this.posts()}
230 {this.state.view == View.Saved && this.overview()}
233 <div class="col-12 col-md-4">
235 {this.isCurrentUser && this.userSettings()}
247 <div class="btn-group btn-group-toggle">
249 className={`btn btn-sm btn-secondary pointer btn-outline-light
250 ${this.state.view == View.Overview && 'active'}
255 value={View.Overview}
256 checked={this.state.view == View.Overview}
257 onChange={linkEvent(this, this.handleViewChange)}
262 className={`btn btn-sm btn-secondary pointer btn-outline-light
263 ${this.state.view == View.Comments && 'active'}
268 value={View.Comments}
269 checked={this.state.view == View.Comments}
270 onChange={linkEvent(this, this.handleViewChange)}
275 className={`btn btn-sm btn-secondary pointer btn-outline-light
276 ${this.state.view == View.Posts && 'active'}
282 checked={this.state.view == View.Posts}
283 onChange={linkEvent(this, this.handleViewChange)}
288 className={`btn btn-sm btn-secondary pointer btn-outline-light
289 ${this.state.view == View.Saved && 'active'}
295 checked={this.state.view == View.Saved}
296 onChange={linkEvent(this, this.handleViewChange)}
306 <div className="mb-2">
307 <span class="mr-3">{this.viewRadios()}</span>
309 sort={this.state.sort}
310 onChange={this.handleSortChange}
314 href={`/feeds/u/${this.state.username}.xml?sort=${
315 SortType[this.state.sort]
320 <svg class="icon mx-2 text-muted small">
321 <use xlinkHref="#icon-rss">#</use>
329 let combined: Array<{ type_: string; data: Comment | Post }> = [];
330 let comments = this.state.comments.map(e => {
331 return { type_: 'comments', data: e };
333 let posts = this.state.posts.map(e => {
334 return { type_: 'posts', data: e };
337 combined.push(...comments);
338 combined.push(...posts);
341 if (this.state.sort == SortType.New) {
342 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
344 combined.sort((a, b) => b.data.score - a.data.score);
351 {i.type_ == 'posts' ? (
353 post={i.data as Post}
354 admins={this.state.admins}
359 nodes={[{ comment: i.data as Comment }]}
360 admins={this.state.admins}
375 nodes={commentsToFlatNodes(this.state.comments)}
376 admins={this.state.admins}
387 {this.state.posts.map(post => (
388 <PostListing post={post} admins={this.state.admins} showCommunity />
395 let user = this.state.user;
398 <div class="card border-secondary mb-3">
399 <div class="card-body">
401 <ul class="list-inline mb-0">
402 <li className="list-inline-item">{user.name}</li>
404 <li className="list-inline-item badge badge-danger">
411 {i18n.t('joined')} <MomentTime data={user} showAgo />
413 <div class="table-responsive mt-1">
414 <table class="table table-bordered table-sm mt-2 mb-0">
417 <td class="text-center" colSpan={2}>
418 {i18n.t('number_of_points', {
419 count: user.post_score + user.comment_score,
427 {i18n.t('number_of_points', { count: user.post_score })}
431 {i18n.t('number_of_posts', { count: user.number_of_posts })}
437 {i18n.t('number_of_points', { count: user.comment_score })}
441 {i18n.t('number_of_comments', {
442 count: user.number_of_comments,
448 {this.isCurrentUser ? (
450 class="btn btn-block btn-secondary mt-3"
451 onClick={linkEvent(this, this.handleLogoutClick)}
458 className={`btn btn-block btn-secondary mt-3 ${!this.state
459 .user.matrix_user_id && 'disabled'}`}
461 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
463 {i18n.t('send_secure_message')}
466 class="btn btn-block btn-secondary mt-3"
467 to={`/create_private_message?recipient_id=${this.state.user.id}`}
469 {i18n.t('send_message')}
482 <div class="card border-secondary mb-3">
483 <div class="card-body">
484 <h5>{i18n.t('settings')}</h5>
485 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
486 <div class="form-group">
487 <label>{i18n.t('avatar')}</label>
488 <form class="d-inline">
490 htmlFor="file-upload"
491 class="pointer ml-4 text-muted small font-weight-bold"
493 {!this.state.userSettingsForm.avatar ? (
494 <span class="btn btn-sm btn-secondary">
495 {i18n.t('upload_avatar')}
501 src={this.state.userSettingsForm.avatar}
502 class="rounded-circle"
509 accept="image/*,video/*"
512 disabled={!UserService.Instance.user}
513 onChange={linkEvent(this, this.handleImageUpload)}
517 <div class="form-group">
518 <label>{i18n.t('language')}</label>
520 value={this.state.userSettingsForm.lang}
521 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
522 class="ml-2 custom-select custom-select-sm w-auto"
524 <option disabled>{i18n.t('language')}</option>
525 <option value="browser">{i18n.t('browser_default')}</option>
526 <option disabled>──</option>
527 {languages.map(lang => (
528 <option value={lang.code}>{lang.name}</option>
532 <div class="form-group">
533 <label>{i18n.t('theme')}</label>
535 value={this.state.userSettingsForm.theme}
536 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
537 class="ml-2 custom-select custom-select-sm w-auto"
539 <option disabled>{i18n.t('theme')}</option>
540 {themes.map(theme => (
541 <option value={theme}>{theme}</option>
545 <form className="form-group">
547 <div class="mr-2">{i18n.t('sort_type')}</div>
550 type_={this.state.userSettingsForm.default_listing_type}
551 onChange={this.handleUserSettingsListingTypeChange}
554 <form className="form-group">
556 <div class="mr-2">{i18n.t('type')}</div>
559 sort={this.state.userSettingsForm.default_sort_type}
560 onChange={this.handleUserSettingsSortTypeChange}
563 <div class="form-group row">
564 <label class="col-lg-3 col-form-label" htmlFor="user-email">
567 <div class="col-lg-9">
572 placeholder={i18n.t('optional')}
573 value={this.state.userSettingsForm.email}
576 this.handleUserSettingsEmailChange
582 <div class="form-group row">
583 <label class="col-lg-5 col-form-label">
584 <a href="https://about.riot.im/" target="_blank">
585 {i18n.t('matrix_user_id')}
588 <div class="col-lg-7">
592 placeholder="@user:example.com"
593 value={this.state.userSettingsForm.matrix_user_id}
596 this.handleUserSettingsMatrixUserIdChange
602 <div class="form-group row">
603 <label class="col-lg-5 col-form-label" htmlFor="user-password">
604 {i18n.t('new_password')}
606 <div class="col-lg-7">
611 value={this.state.userSettingsForm.new_password}
614 this.handleUserSettingsNewPasswordChange
619 <div class="form-group row">
621 class="col-lg-5 col-form-label"
622 htmlFor="user-verify-password"
624 {i18n.t('verify_password')}
626 <div class="col-lg-7">
629 id="user-verify-password"
631 value={this.state.userSettingsForm.new_password_verify}
634 this.handleUserSettingsNewPasswordVerifyChange
639 <div class="form-group row">
641 class="col-lg-5 col-form-label"
642 htmlFor="user-old-password"
644 {i18n.t('old_password')}
646 <div class="col-lg-7">
649 id="user-old-password"
651 value={this.state.userSettingsForm.old_password}
654 this.handleUserSettingsOldPasswordChange
659 {WebSocketService.Instance.site.enable_nsfw && (
660 <div class="form-group">
661 <div class="form-check">
663 class="form-check-input"
666 checked={this.state.userSettingsForm.show_nsfw}
669 this.handleUserSettingsShowNsfwChange
672 <label class="form-check-label" htmlFor="user-show-nsfw">
673 {i18n.t('show_nsfw')}
678 <div class="form-group">
679 <div class="form-check">
681 class="form-check-input"
682 id="user-show-avatars"
684 checked={this.state.userSettingsForm.show_avatars}
687 this.handleUserSettingsShowAvatarsChange
690 <label class="form-check-label" htmlFor="user-show-avatars">
691 {i18n.t('show_avatars')}
695 <div class="form-group">
696 <div class="form-check">
698 class="form-check-input"
699 id="user-send-notifications-to-email"
701 disabled={!this.state.user.email}
703 this.state.userSettingsForm.send_notifications_to_email
707 this.handleUserSettingsSendNotificationsToEmailChange
711 class="form-check-label"
712 htmlFor="user-send-notifications-to-email"
714 {i18n.t('send_notifications_to_email')}
718 <div class="form-group">
719 <button type="submit" class="btn btn-block btn-secondary mr-4">
720 {this.state.userSettingsLoading ? (
721 <svg class="icon icon-spinner spin">
722 <use xlinkHref="#icon-spinner"></use>
725 capitalizeFirstLetter(i18n.t('save'))
730 <div class="form-group mb-0">
732 class="btn btn-block btn-danger"
735 this.handleDeleteAccountShowConfirmToggle
738 {i18n.t('delete_account')}
740 {this.state.deleteAccountShowConfirm && (
742 <div class="my-2 alert alert-danger" role="alert">
743 {i18n.t('delete_account_confirm')}
747 value={this.state.deleteAccountForm.password}
750 this.handleDeleteAccountPasswordChange
752 class="form-control my-2"
755 class="btn btn-danger mr-4"
756 disabled={!this.state.deleteAccountForm.password}
757 onClick={linkEvent(this, this.handleDeleteAccount)}
759 {this.state.deleteAccountLoading ? (
760 <svg class="icon icon-spinner spin">
761 <use xlinkHref="#icon-spinner"></use>
764 capitalizeFirstLetter(i18n.t('delete'))
768 class="btn btn-secondary"
771 this.handleDeleteAccountShowConfirmToggle
789 {this.state.moderates.length > 0 && (
790 <div class="card border-secondary mb-3">
791 <div class="card-body">
792 <h5>{i18n.t('moderates')}</h5>
793 <ul class="list-unstyled mb-0">
794 {this.state.moderates.map(community => (
796 <Link to={`/c/${community.community_name}`}>
797 {community.community_name}
812 {this.state.follows.length > 0 && (
813 <div class="card border-secondary mb-3">
814 <div class="card-body">
815 <h5>{i18n.t('subscribed')}</h5>
816 <ul class="list-unstyled mb-0">
817 {this.state.follows.map(community => (
819 <Link to={`/c/${community.community_name}`}>
820 {community.community_name}
835 {this.state.page > 1 && (
837 class="btn btn-sm btn-secondary mr-1"
838 onClick={linkEvent(this, this.prevPage)}
844 class="btn btn-sm btn-secondary"
845 onClick={linkEvent(this, this.nextPage)}
854 let viewStr = View[this.state.view].toLowerCase();
855 let sortStr = SortType[this.state.sort].toLowerCase();
856 this.props.history.push(
857 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
876 let form: GetUserDetailsForm = {
877 user_id: this.state.user_id,
878 username: this.state.username,
879 sort: SortType[this.state.sort],
880 saved_only: this.state.view == View.Saved,
881 page: this.state.page,
884 WebSocketService.Instance.getUserDetails(form);
887 handleSortChange(val: SortType) {
888 this.state.sort = val;
890 this.setState(this.state);
895 handleViewChange(i: User, event: any) {
896 i.state.view = Number(event.target.value);
903 handleUserSettingsShowNsfwChange(i: User, event: any) {
904 i.state.userSettingsForm.show_nsfw = event.target.checked;
908 handleUserSettingsShowAvatarsChange(i: User, event: any) {
909 i.state.userSettingsForm.show_avatars = event.target.checked;
910 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
914 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
915 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
919 handleUserSettingsThemeChange(i: User, event: any) {
920 i.state.userSettingsForm.theme = event.target.value;
921 setTheme(event.target.value);
925 handleUserSettingsLangChange(i: User, event: any) {
926 i.state.userSettingsForm.lang = event.target.value;
927 i18n.changeLanguage(i.state.userSettingsForm.lang);
931 handleUserSettingsSortTypeChange(val: SortType) {
932 this.state.userSettingsForm.default_sort_type = val;
933 this.setState(this.state);
936 handleUserSettingsListingTypeChange(val: ListingType) {
937 this.state.userSettingsForm.default_listing_type = val;
938 this.setState(this.state);
941 handleUserSettingsEmailChange(i: User, event: any) {
942 i.state.userSettingsForm.email = event.target.value;
943 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
944 i.state.userSettingsForm.email = undefined;
949 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
950 i.state.userSettingsForm.matrix_user_id = event.target.value;
952 i.state.userSettingsForm.matrix_user_id == '' &&
953 !i.state.user.matrix_user_id
955 i.state.userSettingsForm.matrix_user_id = undefined;
960 handleUserSettingsNewPasswordChange(i: User, event: any) {
961 i.state.userSettingsForm.new_password = event.target.value;
962 if (i.state.userSettingsForm.new_password == '') {
963 i.state.userSettingsForm.new_password = undefined;
968 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
969 i.state.userSettingsForm.new_password_verify = event.target.value;
970 if (i.state.userSettingsForm.new_password_verify == '') {
971 i.state.userSettingsForm.new_password_verify = undefined;
976 handleUserSettingsOldPasswordChange(i: User, event: any) {
977 i.state.userSettingsForm.old_password = event.target.value;
978 if (i.state.userSettingsForm.old_password == '') {
979 i.state.userSettingsForm.old_password = undefined;
984 handleImageUpload(i: User, event: any) {
985 event.preventDefault();
986 let file = event.target.files[0];
987 const imageUploadUrl = `/pictshare/api/upload.php`;
988 const formData = new FormData();
989 formData.append('file', file);
991 i.state.avatarLoading = true;
994 fetch(imageUploadUrl, {
998 .then(res => res.json())
1000 let url = `${window.location.origin}/pictshare/${res.url}`;
1001 if (res.filetype == 'mp4') {
1004 i.state.userSettingsForm.avatar = url;
1006 i.state.avatarLoading = false;
1007 i.setState(i.state);
1010 i.state.avatarLoading = false;
1011 i.setState(i.state);
1012 toast(error, 'danger');
1016 handleUserSettingsSubmit(i: User, event: any) {
1017 event.preventDefault();
1018 i.state.userSettingsLoading = true;
1019 i.setState(i.state);
1021 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
1024 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1025 event.preventDefault();
1026 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1027 i.setState(i.state);
1030 handleDeleteAccountPasswordChange(i: User, event: any) {
1031 i.state.deleteAccountForm.password = event.target.value;
1032 i.setState(i.state);
1035 handleLogoutClick(i: User) {
1036 UserService.Instance.logout();
1037 i.context.router.history.push('/');
1040 handleDeleteAccount(i: User, event: any) {
1041 event.preventDefault();
1042 i.state.deleteAccountLoading = true;
1043 i.setState(i.state);
1045 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1048 parseMessage(msg: WebSocketJsonResponse) {
1050 let res = wsJsonToRes(msg);
1052 toast(i18n.t(msg.error), 'danger');
1053 this.state.deleteAccountLoading = false;
1054 this.state.avatarLoading = false;
1055 this.state.userSettingsLoading = false;
1056 if (msg.error == 'couldnt_find_that_username_or_email') {
1057 this.context.router.history.push('/');
1059 this.setState(this.state);
1061 } else if (msg.reconnect) {
1063 } else if (res.op == UserOperation.GetUserDetails) {
1064 let data = res.data as UserDetailsResponse;
1065 this.state.user = data.user;
1066 this.state.comments = data.comments;
1067 this.state.follows = data.follows;
1068 this.state.moderates = data.moderates;
1069 this.state.posts = data.posts;
1070 this.state.admins = data.admins;
1071 this.state.loading = false;
1072 if (this.isCurrentUser) {
1073 this.state.userSettingsForm.show_nsfw =
1074 UserService.Instance.user.show_nsfw;
1075 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1076 ? UserService.Instance.user.theme
1078 this.state.userSettingsForm.default_sort_type =
1079 UserService.Instance.user.default_sort_type;
1080 this.state.userSettingsForm.default_listing_type =
1081 UserService.Instance.user.default_listing_type;
1082 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1083 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1084 this.state.userSettingsForm.email = this.state.user.email;
1085 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1086 this.state.userSettingsForm.show_avatars =
1087 UserService.Instance.user.show_avatars;
1088 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1090 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1091 window.scrollTo(0, 0);
1092 this.setState(this.state);
1094 } else if (res.op == UserOperation.EditComment) {
1095 let data = res.data as CommentResponse;
1096 editCommentRes(data, this.state.comments);
1097 this.setState(this.state);
1098 } else if (res.op == UserOperation.CreateComment) {
1099 let data = res.data as CommentResponse;
1101 UserService.Instance.user &&
1102 data.comment.creator_id == UserService.Instance.user.id
1104 toast(i18n.t('reply_sent'));
1106 } else if (res.op == UserOperation.SaveComment) {
1107 let data = res.data as CommentResponse;
1108 saveCommentRes(data, this.state.comments);
1109 this.setState(this.state);
1110 } else if (res.op == UserOperation.CreateCommentLike) {
1111 let data = res.data as CommentResponse;
1112 createCommentLikeRes(data, this.state.comments);
1113 this.setState(this.state);
1114 } else if (res.op == UserOperation.CreatePostLike) {
1115 let data = res.data as PostResponse;
1116 createPostLikeFindRes(data, this.state.posts);
1117 this.setState(this.state);
1118 } else if (res.op == UserOperation.BanUser) {
1119 let data = res.data as BanUserResponse;
1121 .filter(c => c.creator_id == data.user.id)
1122 .forEach(c => (c.banned = data.banned));
1124 .filter(c => c.creator_id == data.user.id)
1125 .forEach(c => (c.banned = data.banned));
1126 this.setState(this.state);
1127 } else if (res.op == UserOperation.AddAdmin) {
1128 let data = res.data as AddAdminResponse;
1129 this.state.admins = data.admins;
1130 this.setState(this.state);
1131 } else if (res.op == UserOperation.SaveUserSettings) {
1132 let data = res.data as LoginResponse;
1133 this.state = this.emptyState;
1134 this.state.userSettingsLoading = false;
1135 this.setState(this.state);
1136 UserService.Instance.login(data);
1137 } else if (res.op == UserOperation.DeleteAccount) {
1138 this.state.deleteAccountLoading = false;
1139 this.state.deleteAccountShowConfirm = false;
1140 this.setState(this.state);
1141 this.context.router.history.push('/');