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 className="mb-2">
249 value={this.state.view}
250 onChange={linkEvent(this, this.handleViewChange)}
251 class="custom-select custom-select-sm w-auto"
253 <option disabled>{i18n.t('view')}</option>
254 <option value={View.Overview}>{i18n.t('overview')}</option>
255 <option value={View.Comments}>{i18n.t('comments')}</option>
256 <option value={View.Posts}>{i18n.t('posts')}</option>
257 <option value={View.Saved}>{i18n.t('saved')}</option>
261 sort={this.state.sort}
262 onChange={this.handleSortChange}
267 href={`/feeds/u/${this.state.username}.xml?sort=${
268 SortType[this.state.sort]
273 <svg class="icon mx-2 text-muted small">
274 <use xlinkHref="#icon-rss">#</use>
282 let combined: Array<{ type_: string; data: Comment | Post }> = [];
283 let comments = this.state.comments.map(e => {
284 return { type_: 'comments', data: e };
286 let posts = this.state.posts.map(e => {
287 return { type_: 'posts', data: e };
290 combined.push(...comments);
291 combined.push(...posts);
294 if (this.state.sort == SortType.New) {
295 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
297 combined.sort((a, b) => b.data.score - a.data.score);
304 {i.type_ == 'posts' ? (
306 post={i.data as Post}
307 admins={this.state.admins}
312 nodes={[{ comment: i.data as Comment }]}
313 admins={this.state.admins}
328 nodes={commentsToFlatNodes(this.state.comments)}
329 admins={this.state.admins}
340 {this.state.posts.map(post => (
341 <PostListing post={post} admins={this.state.admins} showCommunity />
348 let user = this.state.user;
351 <div class="card border-secondary mb-3">
352 <div class="card-body">
354 <ul class="list-inline mb-0">
355 <li className="list-inline-item">{user.name}</li>
357 <li className="list-inline-item badge badge-danger">
364 {i18n.t('joined')} <MomentTime data={user} showAgo />
366 <div class="table-responsive mt-1">
367 <table class="table table-bordered table-sm mt-2 mb-0">
370 <td class="text-center" colSpan={2}>
371 {i18n.t('number_of_points', {
372 count: user.post_score + user.comment_score,
380 {i18n.t('number_of_points', { count: user.post_score })}
384 {i18n.t('number_of_posts', { count: user.number_of_posts })}
390 {i18n.t('number_of_points', { count: user.comment_score })}
394 {i18n.t('number_of_comments', {
395 count: user.number_of_comments,
401 {this.isCurrentUser ? (
403 class="btn btn-block btn-secondary mt-3"
404 onClick={linkEvent(this, this.handleLogoutClick)}
411 className={`btn btn-block btn-secondary mt-3 ${!this.state
412 .user.matrix_user_id && 'disabled'}`}
414 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
416 {i18n.t('send_secure_message')}
419 class="btn btn-block btn-secondary mt-3"
420 to={`/create_private_message?recipient_id=${this.state.user.id}`}
422 {i18n.t('send_message')}
435 <div class="card border-secondary mb-3">
436 <div class="card-body">
437 <h5>{i18n.t('settings')}</h5>
438 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
439 <div class="form-group">
440 <label>{i18n.t('avatar')}</label>
441 <form class="d-inline">
443 htmlFor="file-upload"
444 class="pointer ml-4 text-muted small font-weight-bold"
446 {!this.state.userSettingsForm.avatar ? (
447 <span class="btn btn-sm btn-secondary">
448 {i18n.t('upload_avatar')}
454 src={this.state.userSettingsForm.avatar}
455 class="rounded-circle"
462 accept="image/*,video/*"
465 disabled={!UserService.Instance.user}
466 onChange={linkEvent(this, this.handleImageUpload)}
470 <div class="form-group">
471 <label>{i18n.t('language')}</label>
473 value={this.state.userSettingsForm.lang}
474 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
475 class="ml-2 custom-select custom-select-sm w-auto"
477 <option disabled>{i18n.t('language')}</option>
478 <option value="browser">{i18n.t('browser_default')}</option>
479 <option disabled>──</option>
480 {languages.map(lang => (
481 <option value={lang.code}>{lang.name}</option>
485 <div class="form-group">
486 <label>{i18n.t('theme')}</label>
488 value={this.state.userSettingsForm.theme}
489 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
490 class="ml-2 custom-select custom-select-sm w-auto"
492 <option disabled>{i18n.t('theme')}</option>
493 {themes.map(theme => (
494 <option value={theme}>{theme}</option>
498 <form className="form-group">
500 <div class="mr-2">{i18n.t('sort_type')}</div>
503 type_={this.state.userSettingsForm.default_listing_type}
504 onChange={this.handleUserSettingsListingTypeChange}
507 <form className="form-group">
509 <div class="mr-2">{i18n.t('type')}</div>
512 sort={this.state.userSettingsForm.default_sort_type}
513 onChange={this.handleUserSettingsSortTypeChange}
516 <div class="form-group row">
517 <label class="col-lg-3 col-form-label" htmlFor="user-email">
520 <div class="col-lg-9">
525 placeholder={i18n.t('optional')}
526 value={this.state.userSettingsForm.email}
529 this.handleUserSettingsEmailChange
535 <div class="form-group row">
536 <label class="col-lg-5 col-form-label">
537 <a href="https://about.riot.im/" target="_blank">
538 {i18n.t('matrix_user_id')}
541 <div class="col-lg-7">
545 placeholder="@user:example.com"
546 value={this.state.userSettingsForm.matrix_user_id}
549 this.handleUserSettingsMatrixUserIdChange
555 <div class="form-group row">
556 <label class="col-lg-5 col-form-label" htmlFor="user-password">
557 {i18n.t('new_password')}
559 <div class="col-lg-7">
564 value={this.state.userSettingsForm.new_password}
567 this.handleUserSettingsNewPasswordChange
572 <div class="form-group row">
574 class="col-lg-5 col-form-label"
575 htmlFor="user-verify-password"
577 {i18n.t('verify_password')}
579 <div class="col-lg-7">
582 id="user-verify-password"
584 value={this.state.userSettingsForm.new_password_verify}
587 this.handleUserSettingsNewPasswordVerifyChange
592 <div class="form-group row">
594 class="col-lg-5 col-form-label"
595 htmlFor="user-old-password"
597 {i18n.t('old_password')}
599 <div class="col-lg-7">
602 id="user-old-password"
604 value={this.state.userSettingsForm.old_password}
607 this.handleUserSettingsOldPasswordChange
612 {WebSocketService.Instance.site.enable_nsfw && (
613 <div class="form-group">
614 <div class="form-check">
616 class="form-check-input"
619 checked={this.state.userSettingsForm.show_nsfw}
622 this.handleUserSettingsShowNsfwChange
625 <label class="form-check-label" htmlFor="user-show-nsfw">
626 {i18n.t('show_nsfw')}
631 <div class="form-group">
632 <div class="form-check">
634 class="form-check-input"
635 id="user-show-avatars"
637 checked={this.state.userSettingsForm.show_avatars}
640 this.handleUserSettingsShowAvatarsChange
643 <label class="form-check-label" htmlFor="user-show-avatars">
644 {i18n.t('show_avatars')}
648 <div class="form-group">
649 <div class="form-check">
651 class="form-check-input"
652 id="user-send-notifications-to-email"
654 disabled={!this.state.user.email}
656 this.state.userSettingsForm.send_notifications_to_email
660 this.handleUserSettingsSendNotificationsToEmailChange
664 class="form-check-label"
665 htmlFor="user-send-notifications-to-email"
667 {i18n.t('send_notifications_to_email')}
671 <div class="form-group">
672 <button type="submit" class="btn btn-block btn-secondary mr-4">
673 {this.state.userSettingsLoading ? (
674 <svg class="icon icon-spinner spin">
675 <use xlinkHref="#icon-spinner"></use>
678 capitalizeFirstLetter(i18n.t('save'))
683 <div class="form-group mb-0">
685 class="btn btn-block btn-danger"
688 this.handleDeleteAccountShowConfirmToggle
691 {i18n.t('delete_account')}
693 {this.state.deleteAccountShowConfirm && (
695 <div class="my-2 alert alert-danger" role="alert">
696 {i18n.t('delete_account_confirm')}
700 value={this.state.deleteAccountForm.password}
703 this.handleDeleteAccountPasswordChange
705 class="form-control my-2"
708 class="btn btn-danger mr-4"
709 disabled={!this.state.deleteAccountForm.password}
710 onClick={linkEvent(this, this.handleDeleteAccount)}
712 {this.state.deleteAccountLoading ? (
713 <svg class="icon icon-spinner spin">
714 <use xlinkHref="#icon-spinner"></use>
717 capitalizeFirstLetter(i18n.t('delete'))
721 class="btn btn-secondary"
724 this.handleDeleteAccountShowConfirmToggle
742 {this.state.moderates.length > 0 && (
743 <div class="card border-secondary mb-3">
744 <div class="card-body">
745 <h5>{i18n.t('moderates')}</h5>
746 <ul class="list-unstyled mb-0">
747 {this.state.moderates.map(community => (
749 <Link to={`/c/${community.community_name}`}>
750 {community.community_name}
765 {this.state.follows.length > 0 && (
766 <div class="card border-secondary mb-3">
767 <div class="card-body">
768 <h5>{i18n.t('subscribed')}</h5>
769 <ul class="list-unstyled mb-0">
770 {this.state.follows.map(community => (
772 <Link to={`/c/${community.community_name}`}>
773 {community.community_name}
788 {this.state.page > 1 && (
790 class="btn btn-sm btn-secondary mr-1"
791 onClick={linkEvent(this, this.prevPage)}
797 class="btn btn-sm btn-secondary"
798 onClick={linkEvent(this, this.nextPage)}
807 let viewStr = View[this.state.view].toLowerCase();
808 let sortStr = SortType[this.state.sort].toLowerCase();
809 this.props.history.push(
810 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
829 let form: GetUserDetailsForm = {
830 user_id: this.state.user_id,
831 username: this.state.username,
832 sort: SortType[this.state.sort],
833 saved_only: this.state.view == View.Saved,
834 page: this.state.page,
837 WebSocketService.Instance.getUserDetails(form);
840 handleSortChange(val: SortType) {
841 this.state.sort = val;
843 this.setState(this.state);
848 handleViewChange(i: User, event: any) {
849 i.state.view = Number(event.target.value);
856 handleUserSettingsShowNsfwChange(i: User, event: any) {
857 i.state.userSettingsForm.show_nsfw = event.target.checked;
861 handleUserSettingsShowAvatarsChange(i: User, event: any) {
862 i.state.userSettingsForm.show_avatars = event.target.checked;
863 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
867 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
868 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
872 handleUserSettingsThemeChange(i: User, event: any) {
873 i.state.userSettingsForm.theme = event.target.value;
874 setTheme(event.target.value);
878 handleUserSettingsLangChange(i: User, event: any) {
879 i.state.userSettingsForm.lang = event.target.value;
880 i18n.changeLanguage(i.state.userSettingsForm.lang);
884 handleUserSettingsSortTypeChange(val: SortType) {
885 this.state.userSettingsForm.default_sort_type = val;
886 this.setState(this.state);
889 handleUserSettingsListingTypeChange(val: ListingType) {
890 this.state.userSettingsForm.default_listing_type = val;
891 this.setState(this.state);
894 handleUserSettingsEmailChange(i: User, event: any) {
895 i.state.userSettingsForm.email = event.target.value;
896 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
897 i.state.userSettingsForm.email = undefined;
902 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
903 i.state.userSettingsForm.matrix_user_id = event.target.value;
905 i.state.userSettingsForm.matrix_user_id == '' &&
906 !i.state.user.matrix_user_id
908 i.state.userSettingsForm.matrix_user_id = undefined;
913 handleUserSettingsNewPasswordChange(i: User, event: any) {
914 i.state.userSettingsForm.new_password = event.target.value;
915 if (i.state.userSettingsForm.new_password == '') {
916 i.state.userSettingsForm.new_password = undefined;
921 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
922 i.state.userSettingsForm.new_password_verify = event.target.value;
923 if (i.state.userSettingsForm.new_password_verify == '') {
924 i.state.userSettingsForm.new_password_verify = undefined;
929 handleUserSettingsOldPasswordChange(i: User, event: any) {
930 i.state.userSettingsForm.old_password = event.target.value;
931 if (i.state.userSettingsForm.old_password == '') {
932 i.state.userSettingsForm.old_password = undefined;
937 handleImageUpload(i: User, event: any) {
938 event.preventDefault();
939 let file = event.target.files[0];
940 const imageUploadUrl = `/pictshare/api/upload.php`;
941 const formData = new FormData();
942 formData.append('file', file);
944 i.state.avatarLoading = true;
947 fetch(imageUploadUrl, {
951 .then(res => res.json())
953 let url = `${window.location.origin}/pictshare/${res.url}`;
954 if (res.filetype == 'mp4') {
957 i.state.userSettingsForm.avatar = url;
959 i.state.avatarLoading = false;
963 i.state.avatarLoading = false;
965 toast(error, 'danger');
969 handleUserSettingsSubmit(i: User, event: any) {
970 event.preventDefault();
971 i.state.userSettingsLoading = true;
974 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
977 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
978 event.preventDefault();
979 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
983 handleDeleteAccountPasswordChange(i: User, event: any) {
984 i.state.deleteAccountForm.password = event.target.value;
988 handleLogoutClick(i: User) {
989 UserService.Instance.logout();
990 i.context.router.history.push('/');
993 handleDeleteAccount(i: User, event: any) {
994 event.preventDefault();
995 i.state.deleteAccountLoading = true;
998 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1001 parseMessage(msg: WebSocketJsonResponse) {
1003 let res = wsJsonToRes(msg);
1005 toast(i18n.t(msg.error), 'danger');
1006 this.state.deleteAccountLoading = false;
1007 this.state.avatarLoading = false;
1008 this.state.userSettingsLoading = false;
1009 if (msg.error == 'couldnt_find_that_username_or_email') {
1010 this.context.router.history.push('/');
1012 this.setState(this.state);
1014 } else if (msg.reconnect) {
1016 } else if (res.op == UserOperation.GetUserDetails) {
1017 let data = res.data as UserDetailsResponse;
1018 this.state.user = data.user;
1019 this.state.comments = data.comments;
1020 this.state.follows = data.follows;
1021 this.state.moderates = data.moderates;
1022 this.state.posts = data.posts;
1023 this.state.admins = data.admins;
1024 this.state.loading = false;
1025 if (this.isCurrentUser) {
1026 this.state.userSettingsForm.show_nsfw =
1027 UserService.Instance.user.show_nsfw;
1028 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1029 ? UserService.Instance.user.theme
1031 this.state.userSettingsForm.default_sort_type =
1032 UserService.Instance.user.default_sort_type;
1033 this.state.userSettingsForm.default_listing_type =
1034 UserService.Instance.user.default_listing_type;
1035 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1036 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1037 this.state.userSettingsForm.email = this.state.user.email;
1038 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1039 this.state.userSettingsForm.show_avatars =
1040 UserService.Instance.user.show_avatars;
1041 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1043 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1044 window.scrollTo(0, 0);
1045 this.setState(this.state);
1047 } else if (res.op == UserOperation.EditComment) {
1048 let data = res.data as CommentResponse;
1049 editCommentRes(data, this.state.comments);
1050 this.setState(this.state);
1051 } else if (res.op == UserOperation.CreateComment) {
1052 let data = res.data as CommentResponse;
1054 UserService.Instance.user &&
1055 data.comment.creator_id == UserService.Instance.user.id
1057 toast(i18n.t('reply_sent'));
1059 } else if (res.op == UserOperation.SaveComment) {
1060 let data = res.data as CommentResponse;
1061 saveCommentRes(data, this.state.comments);
1062 this.setState(this.state);
1063 } else if (res.op == UserOperation.CreateCommentLike) {
1064 let data = res.data as CommentResponse;
1065 createCommentLikeRes(data, this.state.comments);
1066 this.setState(this.state);
1067 } else if (res.op == UserOperation.CreatePostLike) {
1068 let data = res.data as PostResponse;
1069 createPostLikeFindRes(data, this.state.posts);
1070 this.setState(this.state);
1071 } else if (res.op == UserOperation.BanUser) {
1072 let data = res.data as BanUserResponse;
1074 .filter(c => c.creator_id == data.user.id)
1075 .forEach(c => (c.banned = data.banned));
1077 .filter(c => c.creator_id == data.user.id)
1078 .forEach(c => (c.banned = data.banned));
1079 this.setState(this.state);
1080 } else if (res.op == UserOperation.AddAdmin) {
1081 let data = res.data as AddAdminResponse;
1082 this.state.admins = data.admins;
1083 this.setState(this.state);
1084 } else if (res.op == UserOperation.SaveUserSettings) {
1085 let data = res.data as LoginResponse;
1086 this.state = this.emptyState;
1087 this.state.userSettingsLoading = false;
1088 this.setState(this.state);
1089 UserService.Instance.login(data);
1090 } else if (res.op == UserOperation.DeleteAccount) {
1091 this.state.deleteAccountLoading = false;
1092 this.state.deleteAccountShowConfirm = false;
1093 this.setState(this.state);
1094 this.context.router.history.push('/');