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]
271 <svg class="icon mx-2 text-muted small">
272 <use xlinkHref="#icon-rss">#</use>
280 let combined: Array<{ type_: string; data: Comment | Post }> = [];
281 let comments = this.state.comments.map(e => {
282 return { type_: 'comments', data: e };
284 let posts = this.state.posts.map(e => {
285 return { type_: 'posts', data: e };
288 combined.push(...comments);
289 combined.push(...posts);
292 if (this.state.sort == SortType.New) {
293 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
295 combined.sort((a, b) => b.data.score - a.data.score);
302 {i.type_ == 'posts' ? (
304 post={i.data as Post}
305 admins={this.state.admins}
310 nodes={[{ comment: i.data as Comment }]}
311 admins={this.state.admins}
325 nodes={commentsToFlatNodes(this.state.comments)}
326 admins={this.state.admins}
336 {this.state.posts.map(post => (
337 <PostListing post={post} admins={this.state.admins} showCommunity />
344 let user = this.state.user;
347 <div class="card border-secondary mb-3">
348 <div class="card-body">
350 <ul class="list-inline mb-0">
351 <li className="list-inline-item">{user.name}</li>
353 <li className="list-inline-item badge badge-danger">
360 {i18n.t('joined')} <MomentTime data={user} />
362 <div class="table-responsive mt-1">
363 <table class="table table-bordered table-sm mt-2 mb-0">
365 <td class="text-center" colSpan={2}>
366 {i18n.t('number_of_points', {
367 count: user.post_score + user.comment_score,
373 {i18n.t('number_of_points', { count: user.post_score })}
376 {i18n.t('number_of_posts', { count: user.number_of_posts })}
381 {i18n.t('number_of_points', { count: user.comment_score })}
384 {i18n.t('number_of_comments', {
385 count: user.number_of_comments,
391 {this.isCurrentUser ? (
393 class="btn btn-block btn-secondary mt-3"
394 onClick={linkEvent(this, this.handleLogoutClick)}
401 className={`btn btn-block btn-secondary mt-3 ${!this.state
402 .user.matrix_user_id && 'disabled'}`}
404 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
406 {i18n.t('send_secure_message')}
409 class="btn btn-block btn-secondary mt-3"
410 to={`/create_private_message?recipient_id=${this.state.user.id}`}
412 {i18n.t('send_message')}
425 <div class="card border-secondary mb-3">
426 <div class="card-body">
427 <h5>{i18n.t('settings')}</h5>
428 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
429 <div class="form-group">
430 <label>{i18n.t('avatar')}</label>
431 <form class="d-inline">
433 htmlFor="file-upload"
434 class="pointer ml-4 text-muted small font-weight-bold"
436 {!this.state.userSettingsForm.avatar ? (
437 <span class="btn btn-sm btn-secondary">
438 {i18n.t('upload_avatar')}
444 src={this.state.userSettingsForm.avatar}
445 class="rounded-circle"
452 accept="image/*,video/*"
455 disabled={!UserService.Instance.user}
456 onChange={linkEvent(this, this.handleImageUpload)}
460 <div class="form-group">
461 <label>{i18n.t('language')}</label>
463 value={this.state.userSettingsForm.lang}
464 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
465 class="ml-2 custom-select custom-select-sm w-auto"
467 <option disabled>{i18n.t('language')}</option>
468 <option value="browser">{i18n.t('browser_default')}</option>
469 <option disabled>──</option>
470 {languages.map(lang => (
471 <option value={lang.code}>{lang.name}</option>
475 <div class="form-group">
476 <label>{i18n.t('theme')}</label>
478 value={this.state.userSettingsForm.theme}
479 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
480 class="ml-2 custom-select custom-select-sm w-auto"
482 <option disabled>{i18n.t('theme')}</option>
483 {themes.map(theme => (
484 <option value={theme}>{theme}</option>
488 <form className="form-group">
490 <div class="mr-2">{i18n.t('sort_type')}</div>
493 type_={this.state.userSettingsForm.default_listing_type}
494 onChange={this.handleUserSettingsListingTypeChange}
497 <form className="form-group">
499 <div class="mr-2">{i18n.t('type')}</div>
502 sort={this.state.userSettingsForm.default_sort_type}
503 onChange={this.handleUserSettingsSortTypeChange}
506 <div class="form-group row">
507 <label class="col-lg-3 col-form-label" htmlFor="user-email">
510 <div class="col-lg-9">
515 placeholder={i18n.t('optional')}
516 value={this.state.userSettingsForm.email}
519 this.handleUserSettingsEmailChange
525 <div class="form-group row">
526 <label class="col-lg-5 col-form-label">
527 <a href="https://about.riot.im/" target="_blank">
528 {i18n.t('matrix_user_id')}
531 <div class="col-lg-7">
535 placeholder="@user:example.com"
536 value={this.state.userSettingsForm.matrix_user_id}
539 this.handleUserSettingsMatrixUserIdChange
545 <div class="form-group row">
546 <label class="col-lg-5 col-form-label" htmlFor="user-password">
547 {i18n.t('new_password')}
549 <div class="col-lg-7">
554 value={this.state.userSettingsForm.new_password}
557 this.handleUserSettingsNewPasswordChange
562 <div class="form-group row">
564 class="col-lg-5 col-form-label"
565 htmlFor="user-verify-password"
567 {i18n.t('verify_password')}
569 <div class="col-lg-7">
572 id="user-verify-password"
574 value={this.state.userSettingsForm.new_password_verify}
577 this.handleUserSettingsNewPasswordVerifyChange
582 <div class="form-group row">
584 class="col-lg-5 col-form-label"
585 htmlFor="user-old-password"
587 {i18n.t('old_password')}
589 <div class="col-lg-7">
592 id="user-old-password"
594 value={this.state.userSettingsForm.old_password}
597 this.handleUserSettingsOldPasswordChange
602 {WebSocketService.Instance.site.enable_nsfw && (
603 <div class="form-group">
604 <div class="form-check">
606 class="form-check-input"
609 checked={this.state.userSettingsForm.show_nsfw}
612 this.handleUserSettingsShowNsfwChange
615 <label class="form-check-label" htmlFor="user-show-nsfw">
616 {i18n.t('show_nsfw')}
621 <div class="form-group">
622 <div class="form-check">
624 class="form-check-input"
625 id="user-show-avatars"
627 checked={this.state.userSettingsForm.show_avatars}
630 this.handleUserSettingsShowAvatarsChange
633 <label class="form-check-label" htmlFor="user-show-avatars">
634 {i18n.t('show_avatars')}
638 <div class="form-group">
639 <div class="form-check">
641 class="form-check-input"
642 id="user-send-notifications-to-email"
644 disabled={!this.state.user.email}
646 this.state.userSettingsForm.send_notifications_to_email
650 this.handleUserSettingsSendNotificationsToEmailChange
654 class="form-check-label"
655 htmlFor="user-send-notifications-to-email"
657 {i18n.t('send_notifications_to_email')}
661 <div class="form-group">
662 <button type="submit" class="btn btn-block btn-secondary mr-4">
663 {this.state.userSettingsLoading ? (
664 <svg class="icon icon-spinner spin">
665 <use xlinkHref="#icon-spinner"></use>
668 capitalizeFirstLetter(i18n.t('save'))
673 <div class="form-group mb-0">
675 class="btn btn-block btn-danger"
678 this.handleDeleteAccountShowConfirmToggle
681 {i18n.t('delete_account')}
683 {this.state.deleteAccountShowConfirm && (
685 <div class="my-2 alert alert-danger" role="alert">
686 {i18n.t('delete_account_confirm')}
690 value={this.state.deleteAccountForm.password}
693 this.handleDeleteAccountPasswordChange
695 class="form-control my-2"
698 class="btn btn-danger mr-4"
699 disabled={!this.state.deleteAccountForm.password}
700 onClick={linkEvent(this, this.handleDeleteAccount)}
702 {this.state.deleteAccountLoading ? (
703 <svg class="icon icon-spinner spin">
704 <use xlinkHref="#icon-spinner"></use>
707 capitalizeFirstLetter(i18n.t('delete'))
711 class="btn btn-secondary"
714 this.handleDeleteAccountShowConfirmToggle
732 {this.state.moderates.length > 0 && (
733 <div class="card border-secondary mb-3">
734 <div class="card-body">
735 <h5>{i18n.t('moderates')}</h5>
736 <ul class="list-unstyled mb-0">
737 {this.state.moderates.map(community => (
739 <Link to={`/c/${community.community_name}`}>
740 {community.community_name}
755 {this.state.follows.length > 0 && (
756 <div class="card border-secondary mb-3">
757 <div class="card-body">
758 <h5>{i18n.t('subscribed')}</h5>
759 <ul class="list-unstyled mb-0">
760 {this.state.follows.map(community => (
762 <Link to={`/c/${community.community_name}`}>
763 {community.community_name}
778 {this.state.page > 1 && (
780 class="btn btn-sm btn-secondary mr-1"
781 onClick={linkEvent(this, this.prevPage)}
787 class="btn btn-sm btn-secondary"
788 onClick={linkEvent(this, this.nextPage)}
797 let viewStr = View[this.state.view].toLowerCase();
798 let sortStr = SortType[this.state.sort].toLowerCase();
799 this.props.history.push(
800 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
819 let form: GetUserDetailsForm = {
820 user_id: this.state.user_id,
821 username: this.state.username,
822 sort: SortType[this.state.sort],
823 saved_only: this.state.view == View.Saved,
824 page: this.state.page,
827 WebSocketService.Instance.getUserDetails(form);
830 handleSortChange(val: SortType) {
831 this.state.sort = val;
833 this.setState(this.state);
838 handleViewChange(i: User, event: any) {
839 i.state.view = Number(event.target.value);
846 handleUserSettingsShowNsfwChange(i: User, event: any) {
847 i.state.userSettingsForm.show_nsfw = event.target.checked;
851 handleUserSettingsShowAvatarsChange(i: User, event: any) {
852 i.state.userSettingsForm.show_avatars = event.target.checked;
853 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
857 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
858 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
862 handleUserSettingsThemeChange(i: User, event: any) {
863 i.state.userSettingsForm.theme = event.target.value;
864 setTheme(event.target.value);
868 handleUserSettingsLangChange(i: User, event: any) {
869 i.state.userSettingsForm.lang = event.target.value;
870 i18n.changeLanguage(i.state.userSettingsForm.lang);
874 handleUserSettingsSortTypeChange(val: SortType) {
875 this.state.userSettingsForm.default_sort_type = val;
876 this.setState(this.state);
879 handleUserSettingsListingTypeChange(val: ListingType) {
880 this.state.userSettingsForm.default_listing_type = val;
881 this.setState(this.state);
884 handleUserSettingsEmailChange(i: User, event: any) {
885 i.state.userSettingsForm.email = event.target.value;
886 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
887 i.state.userSettingsForm.email = undefined;
892 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
893 i.state.userSettingsForm.matrix_user_id = event.target.value;
895 i.state.userSettingsForm.matrix_user_id == '' &&
896 !i.state.user.matrix_user_id
898 i.state.userSettingsForm.matrix_user_id = undefined;
903 handleUserSettingsNewPasswordChange(i: User, event: any) {
904 i.state.userSettingsForm.new_password = event.target.value;
905 if (i.state.userSettingsForm.new_password == '') {
906 i.state.userSettingsForm.new_password = undefined;
911 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
912 i.state.userSettingsForm.new_password_verify = event.target.value;
913 if (i.state.userSettingsForm.new_password_verify == '') {
914 i.state.userSettingsForm.new_password_verify = undefined;
919 handleUserSettingsOldPasswordChange(i: User, event: any) {
920 i.state.userSettingsForm.old_password = event.target.value;
921 if (i.state.userSettingsForm.old_password == '') {
922 i.state.userSettingsForm.old_password = undefined;
927 handleImageUpload(i: User, event: any) {
928 event.preventDefault();
929 let file = event.target.files[0];
930 const imageUploadUrl = `/pictshare/api/upload.php`;
931 const formData = new FormData();
932 formData.append('file', file);
934 i.state.avatarLoading = true;
937 fetch(imageUploadUrl, {
941 .then(res => res.json())
943 let url = `${window.location.origin}/pictshare/${res.url}`;
944 if (res.filetype == 'mp4') {
947 i.state.userSettingsForm.avatar = url;
949 i.state.avatarLoading = false;
953 i.state.avatarLoading = false;
955 toast(error, 'danger');
959 handleUserSettingsSubmit(i: User, event: any) {
960 event.preventDefault();
961 i.state.userSettingsLoading = true;
964 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
967 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
968 event.preventDefault();
969 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
973 handleDeleteAccountPasswordChange(i: User, event: any) {
974 i.state.deleteAccountForm.password = event.target.value;
978 handleLogoutClick(i: User) {
979 UserService.Instance.logout();
980 i.context.router.history.push('/');
983 handleDeleteAccount(i: User, event: any) {
984 event.preventDefault();
985 i.state.deleteAccountLoading = true;
988 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
991 parseMessage(msg: WebSocketJsonResponse) {
993 let res = wsJsonToRes(msg);
995 toast(i18n.t(msg.error), 'danger');
996 this.state.deleteAccountLoading = false;
997 this.state.avatarLoading = false;
998 this.state.userSettingsLoading = false;
999 if (msg.error == 'couldnt_find_that_username_or_email') {
1000 this.context.router.history.push('/');
1002 this.setState(this.state);
1004 } else if (msg.reconnect) {
1006 } else if (res.op == UserOperation.GetUserDetails) {
1007 let data = res.data as UserDetailsResponse;
1008 this.state.user = data.user;
1009 this.state.comments = data.comments;
1010 this.state.follows = data.follows;
1011 this.state.moderates = data.moderates;
1012 this.state.posts = data.posts;
1013 this.state.admins = data.admins;
1014 this.state.loading = false;
1015 if (this.isCurrentUser) {
1016 this.state.userSettingsForm.show_nsfw =
1017 UserService.Instance.user.show_nsfw;
1018 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1019 ? UserService.Instance.user.theme
1021 this.state.userSettingsForm.default_sort_type =
1022 UserService.Instance.user.default_sort_type;
1023 this.state.userSettingsForm.default_listing_type =
1024 UserService.Instance.user.default_listing_type;
1025 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1026 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1027 this.state.userSettingsForm.email = this.state.user.email;
1028 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1029 this.state.userSettingsForm.show_avatars =
1030 UserService.Instance.user.show_avatars;
1031 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1033 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1034 window.scrollTo(0, 0);
1035 this.setState(this.state);
1036 } else if (res.op == UserOperation.EditComment) {
1037 let data = res.data as CommentResponse;
1038 editCommentRes(data, this.state.comments);
1039 this.setState(this.state);
1040 } else if (res.op == UserOperation.CreateComment) {
1041 let data = res.data as CommentResponse;
1043 UserService.Instance.user &&
1044 data.comment.creator_id == UserService.Instance.user.id
1046 toast(i18n.t('reply_sent'));
1048 } else if (res.op == UserOperation.SaveComment) {
1049 let data = res.data as CommentResponse;
1050 saveCommentRes(data, this.state.comments);
1051 this.setState(this.state);
1052 } else if (res.op == UserOperation.CreateCommentLike) {
1053 let data = res.data as CommentResponse;
1054 createCommentLikeRes(data, this.state.comments);
1055 this.setState(this.state);
1056 } else if (res.op == UserOperation.CreatePostLike) {
1057 let data = res.data as PostResponse;
1058 createPostLikeFindRes(data, this.state.posts);
1059 this.setState(this.state);
1060 } else if (res.op == UserOperation.BanUser) {
1061 let data = res.data as BanUserResponse;
1063 .filter(c => c.creator_id == data.user.id)
1064 .forEach(c => (c.banned = data.banned));
1066 .filter(c => c.creator_id == data.user.id)
1067 .forEach(c => (c.banned = data.banned));
1068 this.setState(this.state);
1069 } else if (res.op == UserOperation.AddAdmin) {
1070 let data = res.data as AddAdminResponse;
1071 this.state.admins = data.admins;
1072 this.setState(this.state);
1073 } else if (res.op == UserOperation.SaveUserSettings) {
1074 let data = res.data as LoginResponse;
1075 this.state = this.emptyState;
1076 this.state.userSettingsLoading = false;
1077 this.setState(this.state);
1078 UserService.Instance.login(data);
1079 } else if (res.op == UserOperation.DeleteAccount) {
1080 this.state.deleteAccountLoading = false;
1081 this.state.deleteAccountShowConfirm = false;
1082 this.setState(this.state);
1083 this.context.router.history.push('/');