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,
41 import { PostListing } from './post-listing';
42 import { SortSelect } from './sort-select';
43 import { ListingTypeSelect } from './listing-type-select';
44 import { CommentNodes } from './comment-nodes';
45 import { MomentTime } from './moment-time';
46 import { i18n } from '../i18next';
59 follows: Array<CommunityUser>;
60 moderates: Array<CommunityUser>;
61 comments: Array<Comment>;
64 admins: Array<UserView>;
69 avatarLoading: boolean;
70 userSettingsForm: UserSettingsForm;
71 userSettingsLoading: boolean;
72 deleteAccountLoading: boolean;
73 deleteAccountShowConfirm: boolean;
74 deleteAccountForm: DeleteAccountForm;
77 export class User extends Component<any, UserState> {
78 private subscription: Subscription;
79 private emptyState: UserState = {
85 number_of_posts: null,
87 number_of_comments: null,
92 send_notifications_to_email: null,
102 avatarLoading: false,
103 view: this.getViewFromProps(this.props),
104 sort: this.getSortTypeFromProps(this.props),
105 page: this.getPageFromProps(this.props),
109 default_sort_type: null,
110 default_listing_type: null,
113 send_notifications_to_email: null,
116 userSettingsLoading: null,
117 deleteAccountLoading: null,
118 deleteAccountShowConfirm: false,
124 constructor(props: any, context: any) {
125 super(props, context);
127 this.state = this.emptyState;
128 this.handleSortChange = this.handleSortChange.bind(this);
129 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
132 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
136 this.state.user_id = Number(this.props.match.params.id);
137 this.state.username = this.props.match.params.username;
139 this.subscription = WebSocketService.Instance.subject
140 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
142 msg => this.parseMessage(msg),
143 err => console.error(err),
144 () => console.log('complete')
150 get isCurrentUser() {
152 UserService.Instance.user &&
153 UserService.Instance.user.id == this.state.user.id
157 getViewFromProps(props: any): View {
158 return props.match.params.view
159 ? View[capitalizeFirstLetter(props.match.params.view)]
163 getSortTypeFromProps(props: any): SortType {
164 return props.match.params.sort
165 ? routeSortTypeToEnum(props.match.params.sort)
169 getPageFromProps(props: any): number {
170 return props.match.params.page ? Number(props.match.params.page) : 1;
173 componentWillUnmount() {
174 this.subscription.unsubscribe();
177 // Necessary for back button for some reason
178 componentWillReceiveProps(nextProps: any) {
180 nextProps.history.action == 'POP' ||
181 nextProps.history.action == 'PUSH'
183 this.state.view = this.getViewFromProps(nextProps);
184 this.state.sort = this.getSortTypeFromProps(nextProps);
185 this.state.page = this.getPageFromProps(nextProps);
186 this.setState(this.state);
191 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
192 // Necessary if you are on a post and you click another post (same route)
194 lastProps.location.pathname.split('/')[2] !==
195 lastProps.history.location.pathname.split('/')[2]
197 // Couldnt get a refresh working. This does for now.
204 <div class="container">
205 {this.state.loading ? (
207 <svg class="icon icon-spinner spin">
208 <use xlinkHref="#icon-spinner"></use>
213 <div class="col-12 col-md-8">
215 {this.state.user.avatar && showAvatars() && (
219 src={this.state.user.avatar}
220 class="rounded-circle mr-2"
223 <span>/u/{this.state.user.name}</span>
226 {this.state.view == View.Overview && this.overview()}
227 {this.state.view == View.Comments && this.comments()}
228 {this.state.view == View.Posts && this.posts()}
229 {this.state.view == View.Saved && this.overview()}
232 <div class="col-12 col-md-4">
234 {this.isCurrentUser && this.userSettings()}
246 <div className="mb-2">
248 value={this.state.view}
249 onChange={linkEvent(this, this.handleViewChange)}
250 class="custom-select custom-select-sm w-auto"
252 <option disabled>{i18n.t('view')}</option>
253 <option value={View.Overview}>{i18n.t('overview')}</option>
254 <option value={View.Comments}>{i18n.t('comments')}</option>
255 <option value={View.Posts}>{i18n.t('posts')}</option>
256 <option value={View.Saved}>{i18n.t('saved')}</option>
260 sort={this.state.sort}
261 onChange={this.handleSortChange}
266 href={`/feeds/u/${this.state.username}.xml?sort=${
267 SortType[this.state.sort]
272 <svg class="icon mx-2 text-muted small">
273 <use xlinkHref="#icon-rss">#</use>
281 let combined: Array<{ type_: string; data: Comment | Post }> = [];
282 let comments = this.state.comments.map(e => {
283 return { type_: 'comments', data: e };
285 let posts = this.state.posts.map(e => {
286 return { type_: 'posts', data: e };
289 combined.push(...comments);
290 combined.push(...posts);
293 if (this.state.sort == SortType.New) {
294 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
296 combined.sort((a, b) => b.data.score - a.data.score);
303 {i.type_ == 'posts' ? (
305 post={i.data as Post}
306 admins={this.state.admins}
311 nodes={[{ comment: i.data as Comment }]}
312 admins={this.state.admins}
326 nodes={commentsToFlatNodes(this.state.comments)}
327 admins={this.state.admins}
337 {this.state.posts.map(post => (
338 <PostListing post={post} admins={this.state.admins} showCommunity />
345 let user = this.state.user;
348 <div class="card border-secondary mb-3">
349 <div class="card-body">
351 <ul class="list-inline mb-0">
352 <li className="list-inline-item">{user.name}</li>
354 <li className="list-inline-item badge badge-danger">
361 {i18n.t('joined')} <MomentTime data={user} />
363 <div class="table-responsive mt-1">
364 <table class="table table-bordered table-sm mt-2 mb-0">
366 <td class="text-center" colSpan={2}>
367 {i18n.t('number_of_points', {
368 count: user.post_score + user.comment_score,
374 {i18n.t('number_of_points', { count: user.post_score })}
377 {i18n.t('number_of_posts', { count: user.number_of_posts })}
382 {i18n.t('number_of_points', { count: user.comment_score })}
385 {i18n.t('number_of_comments', {
386 count: user.number_of_comments,
392 {this.isCurrentUser ? (
394 class="btn btn-block btn-secondary mt-3"
395 onClick={linkEvent(this, this.handleLogoutClick)}
402 className={`btn btn-block btn-secondary mt-3 ${!this.state
403 .user.matrix_user_id && 'disabled'}`}
405 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
407 {i18n.t('send_secure_message')}
410 class="btn btn-block btn-secondary mt-3"
411 to={`/create_private_message?recipient_id=${this.state.user.id}`}
413 {i18n.t('send_message')}
426 <div class="card border-secondary mb-3">
427 <div class="card-body">
428 <h5>{i18n.t('settings')}</h5>
429 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
430 <div class="form-group">
431 <label>{i18n.t('avatar')}</label>
432 <form class="d-inline">
434 htmlFor="file-upload"
435 class="pointer ml-4 text-muted small font-weight-bold"
437 {!this.state.userSettingsForm.avatar ? (
438 <span class="btn btn-sm btn-secondary">
439 {i18n.t('upload_avatar')}
445 src={this.state.userSettingsForm.avatar}
446 class="rounded-circle"
453 accept="image/*,video/*"
456 disabled={!UserService.Instance.user}
457 onChange={linkEvent(this, this.handleImageUpload)}
461 <div class="form-group">
462 <label>{i18n.t('language')}</label>
464 value={this.state.userSettingsForm.lang}
465 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
466 class="ml-2 custom-select custom-select-sm w-auto"
468 <option disabled>{i18n.t('language')}</option>
469 <option value="browser">{i18n.t('browser_default')}</option>
470 <option disabled>──</option>
471 {languages.map(lang => (
472 <option value={lang.code}>{lang.name}</option>
476 <div class="form-group">
477 <label>{i18n.t('theme')}</label>
479 value={this.state.userSettingsForm.theme}
480 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
481 class="ml-2 custom-select custom-select-sm w-auto"
483 <option disabled>{i18n.t('theme')}</option>
484 {themes.map(theme => (
485 <option value={theme}>{theme}</option>
489 <form className="form-group">
491 <div class="mr-2">{i18n.t('sort_type')}</div>
494 type_={this.state.userSettingsForm.default_listing_type}
495 onChange={this.handleUserSettingsListingTypeChange}
498 <form className="form-group">
500 <div class="mr-2">{i18n.t('type')}</div>
503 sort={this.state.userSettingsForm.default_sort_type}
504 onChange={this.handleUserSettingsSortTypeChange}
507 <div class="form-group row">
508 <label class="col-lg-3 col-form-label" htmlFor="user-email">
511 <div class="col-lg-9">
516 placeholder={i18n.t('optional')}
517 value={this.state.userSettingsForm.email}
520 this.handleUserSettingsEmailChange
526 <div class="form-group row">
527 <label class="col-lg-5 col-form-label">
528 <a href="https://about.riot.im/" target="_blank">
529 {i18n.t('matrix_user_id')}
532 <div class="col-lg-7">
536 placeholder="@user:example.com"
537 value={this.state.userSettingsForm.matrix_user_id}
540 this.handleUserSettingsMatrixUserIdChange
546 <div class="form-group row">
547 <label class="col-lg-5 col-form-label" htmlFor="user-password">
548 {i18n.t('new_password')}
550 <div class="col-lg-7">
555 value={this.state.userSettingsForm.new_password}
558 this.handleUserSettingsNewPasswordChange
563 <div class="form-group row">
565 class="col-lg-5 col-form-label"
566 htmlFor="user-verify-password"
568 {i18n.t('verify_password')}
570 <div class="col-lg-7">
573 id="user-verify-password"
575 value={this.state.userSettingsForm.new_password_verify}
578 this.handleUserSettingsNewPasswordVerifyChange
583 <div class="form-group row">
585 class="col-lg-5 col-form-label"
586 htmlFor="user-old-password"
588 {i18n.t('old_password')}
590 <div class="col-lg-7">
593 id="user-old-password"
595 value={this.state.userSettingsForm.old_password}
598 this.handleUserSettingsOldPasswordChange
603 {WebSocketService.Instance.site.enable_nsfw && (
604 <div class="form-group">
605 <div class="form-check">
607 class="form-check-input"
610 checked={this.state.userSettingsForm.show_nsfw}
613 this.handleUserSettingsShowNsfwChange
616 <label class="form-check-label" htmlFor="user-show-nsfw">
617 {i18n.t('show_nsfw')}
622 <div class="form-group">
623 <div class="form-check">
625 class="form-check-input"
626 id="user-show-avatars"
628 checked={this.state.userSettingsForm.show_avatars}
631 this.handleUserSettingsShowAvatarsChange
634 <label class="form-check-label" htmlFor="user-show-avatars">
635 {i18n.t('show_avatars')}
639 <div class="form-group">
640 <div class="form-check">
642 class="form-check-input"
643 id="user-send-notifications-to-email"
645 disabled={!this.state.user.email}
647 this.state.userSettingsForm.send_notifications_to_email
651 this.handleUserSettingsSendNotificationsToEmailChange
655 class="form-check-label"
656 htmlFor="user-send-notifications-to-email"
658 {i18n.t('send_notifications_to_email')}
662 <div class="form-group">
663 <button type="submit" class="btn btn-block btn-secondary mr-4">
664 {this.state.userSettingsLoading ? (
665 <svg class="icon icon-spinner spin">
666 <use xlinkHref="#icon-spinner"></use>
669 capitalizeFirstLetter(i18n.t('save'))
674 <div class="form-group mb-0">
676 class="btn btn-block btn-danger"
679 this.handleDeleteAccountShowConfirmToggle
682 {i18n.t('delete_account')}
684 {this.state.deleteAccountShowConfirm && (
686 <div class="my-2 alert alert-danger" role="alert">
687 {i18n.t('delete_account_confirm')}
691 value={this.state.deleteAccountForm.password}
694 this.handleDeleteAccountPasswordChange
696 class="form-control my-2"
699 class="btn btn-danger mr-4"
700 disabled={!this.state.deleteAccountForm.password}
701 onClick={linkEvent(this, this.handleDeleteAccount)}
703 {this.state.deleteAccountLoading ? (
704 <svg class="icon icon-spinner spin">
705 <use xlinkHref="#icon-spinner"></use>
708 capitalizeFirstLetter(i18n.t('delete'))
712 class="btn btn-secondary"
715 this.handleDeleteAccountShowConfirmToggle
733 {this.state.moderates.length > 0 && (
734 <div class="card border-secondary mb-3">
735 <div class="card-body">
736 <h5>{i18n.t('moderates')}</h5>
737 <ul class="list-unstyled mb-0">
738 {this.state.moderates.map(community => (
740 <Link to={`/c/${community.community_name}`}>
741 {community.community_name}
756 {this.state.follows.length > 0 && (
757 <div class="card border-secondary mb-3">
758 <div class="card-body">
759 <h5>{i18n.t('subscribed')}</h5>
760 <ul class="list-unstyled mb-0">
761 {this.state.follows.map(community => (
763 <Link to={`/c/${community.community_name}`}>
764 {community.community_name}
779 {this.state.page > 1 && (
781 class="btn btn-sm btn-secondary mr-1"
782 onClick={linkEvent(this, this.prevPage)}
788 class="btn btn-sm btn-secondary"
789 onClick={linkEvent(this, this.nextPage)}
798 let viewStr = View[this.state.view].toLowerCase();
799 let sortStr = SortType[this.state.sort].toLowerCase();
800 this.props.history.push(
801 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
820 let form: GetUserDetailsForm = {
821 user_id: this.state.user_id,
822 username: this.state.username,
823 sort: SortType[this.state.sort],
824 saved_only: this.state.view == View.Saved,
825 page: this.state.page,
828 WebSocketService.Instance.getUserDetails(form);
831 handleSortChange(val: SortType) {
832 this.state.sort = val;
834 this.setState(this.state);
839 handleViewChange(i: User, event: any) {
840 i.state.view = Number(event.target.value);
847 handleUserSettingsShowNsfwChange(i: User, event: any) {
848 i.state.userSettingsForm.show_nsfw = event.target.checked;
852 handleUserSettingsShowAvatarsChange(i: User, event: any) {
853 i.state.userSettingsForm.show_avatars = event.target.checked;
854 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
858 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
859 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
863 handleUserSettingsThemeChange(i: User, event: any) {
864 i.state.userSettingsForm.theme = event.target.value;
865 setTheme(event.target.value);
869 handleUserSettingsLangChange(i: User, event: any) {
870 i.state.userSettingsForm.lang = event.target.value;
871 i18n.changeLanguage(i.state.userSettingsForm.lang);
875 handleUserSettingsSortTypeChange(val: SortType) {
876 this.state.userSettingsForm.default_sort_type = val;
877 this.setState(this.state);
880 handleUserSettingsListingTypeChange(val: ListingType) {
881 this.state.userSettingsForm.default_listing_type = val;
882 this.setState(this.state);
885 handleUserSettingsEmailChange(i: User, event: any) {
886 i.state.userSettingsForm.email = event.target.value;
887 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
888 i.state.userSettingsForm.email = undefined;
893 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
894 i.state.userSettingsForm.matrix_user_id = event.target.value;
896 i.state.userSettingsForm.matrix_user_id == '' &&
897 !i.state.user.matrix_user_id
899 i.state.userSettingsForm.matrix_user_id = undefined;
904 handleUserSettingsNewPasswordChange(i: User, event: any) {
905 i.state.userSettingsForm.new_password = event.target.value;
906 if (i.state.userSettingsForm.new_password == '') {
907 i.state.userSettingsForm.new_password = undefined;
912 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
913 i.state.userSettingsForm.new_password_verify = event.target.value;
914 if (i.state.userSettingsForm.new_password_verify == '') {
915 i.state.userSettingsForm.new_password_verify = undefined;
920 handleUserSettingsOldPasswordChange(i: User, event: any) {
921 i.state.userSettingsForm.old_password = event.target.value;
922 if (i.state.userSettingsForm.old_password == '') {
923 i.state.userSettingsForm.old_password = undefined;
928 handleImageUpload(i: User, event: any) {
929 event.preventDefault();
930 let file = event.target.files[0];
931 const imageUploadUrl = `/pictshare/api/upload.php`;
932 const formData = new FormData();
933 formData.append('file', file);
935 i.state.avatarLoading = true;
938 fetch(imageUploadUrl, {
942 .then(res => res.json())
944 let url = `${window.location.origin}/pictshare/${res.url}`;
945 if (res.filetype == 'mp4') {
948 i.state.userSettingsForm.avatar = url;
950 i.state.avatarLoading = false;
954 i.state.avatarLoading = false;
956 toast(error, 'danger');
960 handleUserSettingsSubmit(i: User, event: any) {
961 event.preventDefault();
962 i.state.userSettingsLoading = true;
965 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
968 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
969 event.preventDefault();
970 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
974 handleDeleteAccountPasswordChange(i: User, event: any) {
975 i.state.deleteAccountForm.password = event.target.value;
979 handleLogoutClick(i: User) {
980 UserService.Instance.logout();
981 i.context.router.history.push('/');
984 handleDeleteAccount(i: User, event: any) {
985 event.preventDefault();
986 i.state.deleteAccountLoading = true;
989 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
992 parseMessage(msg: WebSocketJsonResponse) {
994 let res = wsJsonToRes(msg);
996 toast(i18n.t(msg.error), 'danger');
997 this.state.deleteAccountLoading = false;
998 this.state.avatarLoading = false;
999 this.state.userSettingsLoading = false;
1000 if (msg.error == 'couldnt_find_that_username_or_email') {
1001 this.context.router.history.push('/');
1003 this.setState(this.state);
1005 } else if (msg.reconnect) {
1007 } else if (res.op == UserOperation.GetUserDetails) {
1008 let data = res.data as UserDetailsResponse;
1009 this.state.user = data.user;
1010 this.state.comments = data.comments;
1011 this.state.follows = data.follows;
1012 this.state.moderates = data.moderates;
1013 this.state.posts = data.posts;
1014 this.state.admins = data.admins;
1015 this.state.loading = false;
1016 if (this.isCurrentUser) {
1017 this.state.userSettingsForm.show_nsfw =
1018 UserService.Instance.user.show_nsfw;
1019 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1020 ? UserService.Instance.user.theme
1022 this.state.userSettingsForm.default_sort_type =
1023 UserService.Instance.user.default_sort_type;
1024 this.state.userSettingsForm.default_listing_type =
1025 UserService.Instance.user.default_listing_type;
1026 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1027 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1028 this.state.userSettingsForm.email = this.state.user.email;
1029 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1030 this.state.userSettingsForm.show_avatars =
1031 UserService.Instance.user.show_avatars;
1032 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1034 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1035 window.scrollTo(0, 0);
1036 this.setState(this.state);
1037 } else if (res.op == UserOperation.EditComment) {
1038 let data = res.data as CommentResponse;
1039 editCommentRes(data, this.state.comments);
1040 this.setState(this.state);
1041 } else if (res.op == UserOperation.CreateComment) {
1042 let data = res.data as CommentResponse;
1044 UserService.Instance.user &&
1045 data.comment.creator_id == UserService.Instance.user.id
1047 toast(i18n.t('reply_sent'));
1049 } else if (res.op == UserOperation.SaveComment) {
1050 let data = res.data as CommentResponse;
1051 saveCommentRes(data, this.state.comments);
1052 this.setState(this.state);
1053 } else if (res.op == UserOperation.CreateCommentLike) {
1054 let data = res.data as CommentResponse;
1055 createCommentLikeRes(data, this.state.comments);
1056 this.setState(this.state);
1057 } else if (res.op == UserOperation.CreatePostLike) {
1058 let data = res.data as PostResponse;
1059 createPostLikeFindRes(data, this.state.posts);
1060 this.setState(this.state);
1061 } else if (res.op == UserOperation.BanUser) {
1062 let data = res.data as BanUserResponse;
1064 .filter(c => c.creator_id == data.user.id)
1065 .forEach(c => (c.banned = data.banned));
1067 .filter(c => c.creator_id == data.user.id)
1068 .forEach(c => (c.banned = data.banned));
1069 this.setState(this.state);
1070 } else if (res.op == UserOperation.AddAdmin) {
1071 let data = res.data as AddAdminResponse;
1072 this.state.admins = data.admins;
1073 this.setState(this.state);
1074 } else if (res.op == UserOperation.SaveUserSettings) {
1075 let data = res.data as LoginResponse;
1076 this.state = this.emptyState;
1077 this.state.userSettingsLoading = false;
1078 this.setState(this.state);
1079 UserService.Instance.login(data);
1080 } else if (res.op == UserOperation.DeleteAccount) {
1081 this.state.deleteAccountLoading = false;
1082 this.state.deleteAccountShowConfirm = false;
1083 this.setState(this.state);
1084 this.context.router.history.push('/');