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,
36 import { PostListing } from './post-listing';
37 import { SortSelect } from './sort-select';
38 import { ListingTypeSelect } from './listing-type-select';
39 import { CommentNodes } from './comment-nodes';
40 import { MomentTime } from './moment-time';
41 import { i18n } from '../i18next';
54 follows: Array<CommunityUser>;
55 moderates: Array<CommunityUser>;
56 comments: Array<Comment>;
59 admins: Array<UserView>;
64 avatarLoading: boolean;
65 userSettingsForm: UserSettingsForm;
66 userSettingsLoading: boolean;
67 deleteAccountLoading: boolean;
68 deleteAccountShowConfirm: boolean;
69 deleteAccountForm: DeleteAccountForm;
72 export class User extends Component<any, UserState> {
73 private subscription: Subscription;
74 private emptyState: UserState = {
80 number_of_posts: null,
82 number_of_comments: null,
87 send_notifications_to_email: null,
98 view: this.getViewFromProps(this.props),
99 sort: this.getSortTypeFromProps(this.props),
100 page: this.getPageFromProps(this.props),
104 default_sort_type: null,
105 default_listing_type: null,
108 send_notifications_to_email: null,
111 userSettingsLoading: null,
112 deleteAccountLoading: null,
113 deleteAccountShowConfirm: false,
119 constructor(props: any, context: any) {
120 super(props, context);
122 this.state = this.emptyState;
123 this.handleSortChange = this.handleSortChange.bind(this);
124 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
127 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
131 this.state.user_id = Number(this.props.match.params.id);
132 this.state.username = this.props.match.params.username;
134 this.subscription = WebSocketService.Instance.subject
135 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
137 msg => this.parseMessage(msg),
138 err => console.error(err),
139 () => console.log('complete')
145 get isCurrentUser() {
147 UserService.Instance.user &&
148 UserService.Instance.user.id == this.state.user.id
152 getViewFromProps(props: any): View {
153 return props.match.params.view
154 ? View[capitalizeFirstLetter(props.match.params.view)]
158 getSortTypeFromProps(props: any): SortType {
159 return props.match.params.sort
160 ? routeSortTypeToEnum(props.match.params.sort)
164 getPageFromProps(props: any): number {
165 return props.match.params.page ? Number(props.match.params.page) : 1;
168 componentWillUnmount() {
169 this.subscription.unsubscribe();
172 // Necessary for back button for some reason
173 componentWillReceiveProps(nextProps: any) {
175 nextProps.history.action == 'POP' ||
176 nextProps.history.action == 'PUSH'
178 this.state.view = this.getViewFromProps(nextProps);
179 this.state.sort = this.getSortTypeFromProps(nextProps);
180 this.state.page = this.getPageFromProps(nextProps);
181 this.setState(this.state);
186 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
187 // Necessary if you are on a post and you click another post (same route)
189 lastProps.location.pathname.split('/')[2] !==
190 lastProps.history.location.pathname.split('/')[2]
192 // Couldnt get a refresh working. This does for now.
199 <div class="container">
200 {this.state.loading ? (
202 <svg class="icon icon-spinner spin">
203 <use xlinkHref="#icon-spinner"></use>
208 <div class="col-12 col-md-8">
210 {this.state.user.avatar && showAvatars() && (
214 src={this.state.user.avatar}
215 class="rounded-circle mr-2"
218 <span>/u/{this.state.user.name}</span>
221 {this.state.view == View.Overview && this.overview()}
222 {this.state.view == View.Comments && this.comments()}
223 {this.state.view == View.Posts && this.posts()}
224 {this.state.view == View.Saved && this.overview()}
227 <div class="col-12 col-md-4">
229 {this.isCurrentUser && this.userSettings()}
241 <div className="mb-2">
243 value={this.state.view}
244 onChange={linkEvent(this, this.handleViewChange)}
245 class="custom-select custom-select-sm w-auto"
247 <option disabled>{i18n.t('view')}</option>
248 <option value={View.Overview}>{i18n.t('overview')}</option>
249 <option value={View.Comments}>{i18n.t('comments')}</option>
250 <option value={View.Posts}>{i18n.t('posts')}</option>
251 <option value={View.Saved}>{i18n.t('saved')}</option>
255 sort={this.state.sort}
256 onChange={this.handleSortChange}
261 href={`/feeds/u/${this.state.username}.xml?sort=${
262 SortType[this.state.sort]
266 <svg class="icon mx-2 text-muted small">
267 <use xlinkHref="#icon-rss">#</use>
275 let combined: Array<{ type_: string; data: Comment | Post }> = [];
276 let comments = this.state.comments.map(e => {
277 return { type_: 'comments', data: e };
279 let posts = this.state.posts.map(e => {
280 return { type_: 'posts', data: e };
283 combined.push(...comments);
284 combined.push(...posts);
287 if (this.state.sort == SortType.New) {
288 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
290 combined.sort((a, b) => b.data.score - a.data.score);
297 {i.type_ == 'posts' ? (
299 post={i.data as Post}
300 admins={this.state.admins}
305 nodes={[{ comment: i.data as Comment }]}
306 admins={this.state.admins}
319 {this.state.comments.map(comment => (
321 nodes={[{ comment: comment }]}
322 admins={this.state.admins}
333 {this.state.posts.map(post => (
334 <PostListing post={post} admins={this.state.admins} showCommunity />
341 let user = this.state.user;
344 <div class="card border-secondary mb-3">
345 <div class="card-body">
347 <ul class="list-inline mb-0">
348 <li className="list-inline-item">{user.name}</li>
350 <li className="list-inline-item badge badge-danger">
357 {i18n.t('joined')} <MomentTime data={user} />
359 <div class="table-responsive">
360 <table class="table table-bordered table-sm mt-2 mb-0">
363 {i18n.t('number_of_points', { count: user.post_score })}
366 {i18n.t('number_of_posts', { count: user.number_of_posts })}
371 {i18n.t('number_of_points', { count: user.comment_score })}
374 {i18n.t('number_of_comments', {
375 count: user.number_of_comments,
381 {this.isCurrentUser ? (
383 class="btn btn-block btn-secondary mt-3"
384 onClick={linkEvent(this, this.handleLogoutClick)}
391 className={`btn btn-block btn-secondary mt-3 ${!this.state
392 .user.matrix_user_id && 'disabled'}`}
394 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
396 {i18n.t('send_secure_message')}
399 class="btn btn-block btn-secondary mt-3"
400 to={`/create_private_message?recipient_id=${this.state.user.id}`}
402 {i18n.t('send_message')}
415 <div class="card border-secondary mb-3">
416 <div class="card-body">
417 <h5>{i18n.t('settings')}</h5>
418 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
419 <div class="form-group">
420 <label>{i18n.t('avatar')}</label>
421 <form class="d-inline">
423 htmlFor="file-upload"
424 class="pointer ml-4 text-muted small font-weight-bold"
426 {!this.state.userSettingsForm.avatar ? (
427 <span class="btn btn-sm btn-secondary">
428 {i18n.t('upload_avatar')}
434 src={this.state.userSettingsForm.avatar}
435 class="rounded-circle"
442 accept="image/*,video/*"
445 disabled={!UserService.Instance.user}
446 onChange={linkEvent(this, this.handleImageUpload)}
450 <div class="form-group">
451 <label>{i18n.t('language')}</label>
453 value={this.state.userSettingsForm.lang}
454 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
455 class="ml-2 custom-select custom-select-sm w-auto"
457 <option disabled>{i18n.t('language')}</option>
458 <option value="browser">{i18n.t('browser_default')}</option>
459 <option disabled>──</option>
460 {languages.map(lang => (
461 <option value={lang.code}>{lang.name}</option>
465 <div class="form-group">
466 <label>{i18n.t('theme')}</label>
468 value={this.state.userSettingsForm.theme}
469 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
470 class="ml-2 custom-select custom-select-sm w-auto"
472 <option disabled>{i18n.t('theme')}</option>
473 {themes.map(theme => (
474 <option value={theme}>{theme}</option>
478 <form className="form-group">
480 <div class="mr-2">{i18n.t('sort_type')}</div>
483 type_={this.state.userSettingsForm.default_listing_type}
484 onChange={this.handleUserSettingsListingTypeChange}
487 <form className="form-group">
489 <div class="mr-2">{i18n.t('type')}</div>
492 sort={this.state.userSettingsForm.default_sort_type}
493 onChange={this.handleUserSettingsSortTypeChange}
496 <div class="form-group row">
497 <label class="col-lg-3 col-form-label" htmlFor="user-email">
500 <div class="col-lg-9">
505 placeholder={i18n.t('optional')}
506 value={this.state.userSettingsForm.email}
509 this.handleUserSettingsEmailChange
515 <div class="form-group row">
516 <label class="col-lg-5 col-form-label">
517 <a href="https://about.riot.im/" target="_blank">
518 {i18n.t('matrix_user_id')}
521 <div class="col-lg-7">
525 placeholder="@user:example.com"
526 value={this.state.userSettingsForm.matrix_user_id}
529 this.handleUserSettingsMatrixUserIdChange
535 <div class="form-group row">
536 <label class="col-lg-5 col-form-label" htmlFor="user-password">
537 {i18n.t('new_password')}
539 <div class="col-lg-7">
544 value={this.state.userSettingsForm.new_password}
547 this.handleUserSettingsNewPasswordChange
552 <div class="form-group row">
554 class="col-lg-5 col-form-label"
555 htmlFor="user-verify-password"
557 {i18n.t('verify_password')}
559 <div class="col-lg-7">
562 id="user-verify-password"
564 value={this.state.userSettingsForm.new_password_verify}
567 this.handleUserSettingsNewPasswordVerifyChange
572 <div class="form-group row">
574 class="col-lg-5 col-form-label"
575 htmlFor="user-old-password"
577 {i18n.t('old_password')}
579 <div class="col-lg-7">
582 id="user-old-password"
584 value={this.state.userSettingsForm.old_password}
587 this.handleUserSettingsOldPasswordChange
592 {WebSocketService.Instance.site.enable_nsfw && (
593 <div class="form-group">
594 <div class="form-check">
596 class="form-check-input"
599 checked={this.state.userSettingsForm.show_nsfw}
602 this.handleUserSettingsShowNsfwChange
605 <label class="form-check-label" htmlFor="user-show-nsfw">
606 {i18n.t('show_nsfw')}
611 <div class="form-group">
612 <div class="form-check">
614 class="form-check-input"
615 id="user-show-avatars"
617 checked={this.state.userSettingsForm.show_avatars}
620 this.handleUserSettingsShowAvatarsChange
623 <label class="form-check-label" htmlFor="user-show-avatars">
624 {i18n.t('show_avatars')}
628 <div class="form-group">
629 <div class="form-check">
631 class="form-check-input"
632 id="user-send-notifications-to-email"
634 disabled={!this.state.user.email}
636 this.state.userSettingsForm.send_notifications_to_email
640 this.handleUserSettingsSendNotificationsToEmailChange
644 class="form-check-label"
645 htmlFor="user-send-notifications-to-email"
647 {i18n.t('send_notifications_to_email')}
651 <div class="form-group">
652 <button type="submit" class="btn btn-block btn-secondary mr-4">
653 {this.state.userSettingsLoading ? (
654 <svg class="icon icon-spinner spin">
655 <use xlinkHref="#icon-spinner"></use>
658 capitalizeFirstLetter(i18n.t('save'))
663 <div class="form-group mb-0">
665 class="btn btn-block btn-danger"
668 this.handleDeleteAccountShowConfirmToggle
671 {i18n.t('delete_account')}
673 {this.state.deleteAccountShowConfirm && (
675 <div class="my-2 alert alert-danger" role="alert">
676 {i18n.t('delete_account_confirm')}
680 value={this.state.deleteAccountForm.password}
683 this.handleDeleteAccountPasswordChange
685 class="form-control my-2"
688 class="btn btn-danger mr-4"
689 disabled={!this.state.deleteAccountForm.password}
690 onClick={linkEvent(this, this.handleDeleteAccount)}
692 {this.state.deleteAccountLoading ? (
693 <svg class="icon icon-spinner spin">
694 <use xlinkHref="#icon-spinner"></use>
697 capitalizeFirstLetter(i18n.t('delete'))
701 class="btn btn-secondary"
704 this.handleDeleteAccountShowConfirmToggle
722 {this.state.moderates.length > 0 && (
723 <div class="card border-secondary mb-3">
724 <div class="card-body">
725 <h5>{i18n.t('moderates')}</h5>
726 <ul class="list-unstyled mb-0">
727 {this.state.moderates.map(community => (
729 <Link to={`/c/${community.community_name}`}>
730 {community.community_name}
745 {this.state.follows.length > 0 && (
746 <div class="card border-secondary mb-3">
747 <div class="card-body">
748 <h5>{i18n.t('subscribed')}</h5>
749 <ul class="list-unstyled mb-0">
750 {this.state.follows.map(community => (
752 <Link to={`/c/${community.community_name}`}>
753 {community.community_name}
768 {this.state.page > 1 && (
770 class="btn btn-sm btn-secondary mr-1"
771 onClick={linkEvent(this, this.prevPage)}
777 class="btn btn-sm btn-secondary"
778 onClick={linkEvent(this, this.nextPage)}
787 let viewStr = View[this.state.view].toLowerCase();
788 let sortStr = SortType[this.state.sort].toLowerCase();
789 this.props.history.push(
790 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
809 let form: GetUserDetailsForm = {
810 user_id: this.state.user_id,
811 username: this.state.username,
812 sort: SortType[this.state.sort],
813 saved_only: this.state.view == View.Saved,
814 page: this.state.page,
817 WebSocketService.Instance.getUserDetails(form);
820 handleSortChange(val: SortType) {
821 this.state.sort = val;
823 this.setState(this.state);
828 handleViewChange(i: User, event: any) {
829 i.state.view = Number(event.target.value);
836 handleUserSettingsShowNsfwChange(i: User, event: any) {
837 i.state.userSettingsForm.show_nsfw = event.target.checked;
841 handleUserSettingsShowAvatarsChange(i: User, event: any) {
842 i.state.userSettingsForm.show_avatars = event.target.checked;
843 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
847 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
848 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
852 handleUserSettingsThemeChange(i: User, event: any) {
853 i.state.userSettingsForm.theme = event.target.value;
854 setTheme(event.target.value);
858 handleUserSettingsLangChange(i: User, event: any) {
859 i.state.userSettingsForm.lang = event.target.value;
860 i18n.changeLanguage(i.state.userSettingsForm.lang);
864 handleUserSettingsSortTypeChange(val: SortType) {
865 this.state.userSettingsForm.default_sort_type = val;
866 this.setState(this.state);
869 handleUserSettingsListingTypeChange(val: ListingType) {
870 this.state.userSettingsForm.default_listing_type = val;
871 this.setState(this.state);
874 handleUserSettingsEmailChange(i: User, event: any) {
875 i.state.userSettingsForm.email = event.target.value;
876 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
877 i.state.userSettingsForm.email = undefined;
882 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
883 i.state.userSettingsForm.matrix_user_id = event.target.value;
885 i.state.userSettingsForm.matrix_user_id == '' &&
886 !i.state.user.matrix_user_id
888 i.state.userSettingsForm.matrix_user_id = undefined;
893 handleUserSettingsNewPasswordChange(i: User, event: any) {
894 i.state.userSettingsForm.new_password = event.target.value;
895 if (i.state.userSettingsForm.new_password == '') {
896 i.state.userSettingsForm.new_password = undefined;
901 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
902 i.state.userSettingsForm.new_password_verify = event.target.value;
903 if (i.state.userSettingsForm.new_password_verify == '') {
904 i.state.userSettingsForm.new_password_verify = undefined;
909 handleUserSettingsOldPasswordChange(i: User, event: any) {
910 i.state.userSettingsForm.old_password = event.target.value;
911 if (i.state.userSettingsForm.old_password == '') {
912 i.state.userSettingsForm.old_password = undefined;
917 handleImageUpload(i: User, event: any) {
918 event.preventDefault();
919 let file = event.target.files[0];
920 const imageUploadUrl = `/pictshare/api/upload.php`;
921 const formData = new FormData();
922 formData.append('file', file);
924 i.state.avatarLoading = true;
927 fetch(imageUploadUrl, {
931 .then(res => res.json())
933 let url = `${window.location.origin}/pictshare/${res.url}`;
934 if (res.filetype == 'mp4') {
937 i.state.userSettingsForm.avatar = url;
939 i.state.avatarLoading = false;
943 i.state.avatarLoading = false;
945 toast(error, 'danger');
949 handleUserSettingsSubmit(i: User, event: any) {
950 event.preventDefault();
951 i.state.userSettingsLoading = true;
954 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
957 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
958 event.preventDefault();
959 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
963 handleDeleteAccountPasswordChange(i: User, event: any) {
964 i.state.deleteAccountForm.password = event.target.value;
968 handleLogoutClick(i: User) {
969 UserService.Instance.logout();
970 i.context.router.history.push('/');
973 handleDeleteAccount(i: User, event: any) {
974 event.preventDefault();
975 i.state.deleteAccountLoading = true;
978 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
981 parseMessage(msg: WebSocketJsonResponse) {
983 let res = wsJsonToRes(msg);
985 toast(i18n.t(msg.error), 'danger');
986 this.state.deleteAccountLoading = false;
987 this.state.avatarLoading = false;
988 this.state.userSettingsLoading = false;
989 if (msg.error == 'couldnt_find_that_username_or_email') {
990 this.context.router.history.push('/');
992 this.setState(this.state);
994 } else if (res.op == UserOperation.GetUserDetails) {
995 let data = res.data as UserDetailsResponse;
996 this.state.user = data.user;
997 this.state.comments = data.comments;
998 this.state.follows = data.follows;
999 this.state.moderates = data.moderates;
1000 this.state.posts = data.posts;
1001 this.state.admins = data.admins;
1002 this.state.loading = false;
1003 if (this.isCurrentUser) {
1004 this.state.userSettingsForm.show_nsfw =
1005 UserService.Instance.user.show_nsfw;
1006 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1007 ? UserService.Instance.user.theme
1009 this.state.userSettingsForm.default_sort_type =
1010 UserService.Instance.user.default_sort_type;
1011 this.state.userSettingsForm.default_listing_type =
1012 UserService.Instance.user.default_listing_type;
1013 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1014 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1015 this.state.userSettingsForm.email = this.state.user.email;
1016 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1017 this.state.userSettingsForm.show_avatars =
1018 UserService.Instance.user.show_avatars;
1019 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1021 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1022 window.scrollTo(0, 0);
1023 this.setState(this.state);
1024 } else if (res.op == UserOperation.EditComment) {
1025 let data = res.data as CommentResponse;
1027 let found = this.state.comments.find(c => c.id == data.comment.id);
1028 found.content = data.comment.content;
1029 found.updated = data.comment.updated;
1030 found.removed = data.comment.removed;
1031 found.deleted = data.comment.deleted;
1032 found.upvotes = data.comment.upvotes;
1033 found.downvotes = data.comment.downvotes;
1034 found.score = data.comment.score;
1036 this.setState(this.state);
1037 } else if (res.op == UserOperation.CreateComment) {
1038 // let res: CommentResponse = msg;
1039 toast(i18n.t('reply_sent'));
1040 // this.state.comments.unshift(res.comment); // TODO do this right
1041 // this.setState(this.state);
1042 } else if (res.op == UserOperation.SaveComment) {
1043 let data = res.data as CommentResponse;
1044 let found = this.state.comments.find(c => c.id == data.comment.id);
1045 found.saved = data.comment.saved;
1046 this.setState(this.state);
1047 } else if (res.op == UserOperation.CreateCommentLike) {
1048 let data = res.data as CommentResponse;
1049 let found: Comment = this.state.comments.find(
1050 c => c.id === data.comment.id
1052 found.score = data.comment.score;
1053 found.upvotes = data.comment.upvotes;
1054 found.downvotes = data.comment.downvotes;
1055 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1056 this.setState(this.state);
1057 } else if (res.op == UserOperation.CreatePostLike) {
1058 let data = res.data as PostResponse;
1059 let found = this.state.posts.find(c => c.id == data.post.id);
1060 found.my_vote = data.post.my_vote;
1061 found.score = data.post.score;
1062 found.upvotes = data.post.upvotes;
1063 found.downvotes = data.post.downvotes;
1064 this.setState(this.state);
1065 } else if (res.op == UserOperation.BanUser) {
1066 let data = res.data as BanUserResponse;
1068 .filter(c => c.creator_id == data.user.id)
1069 .forEach(c => (c.banned = data.banned));
1071 .filter(c => c.creator_id == data.user.id)
1072 .forEach(c => (c.banned = data.banned));
1073 this.setState(this.state);
1074 } else if (res.op == UserOperation.AddAdmin) {
1075 let data = res.data as AddAdminResponse;
1076 this.state.admins = data.admins;
1077 this.setState(this.state);
1078 } else if (res.op == UserOperation.SaveUserSettings) {
1079 let data = res.data as LoginResponse;
1080 this.state = this.emptyState;
1081 this.state.userSettingsLoading = false;
1082 this.setState(this.state);
1083 UserService.Instance.login(data);
1084 } else if (res.op == UserOperation.DeleteAccount) {
1085 this.state.deleteAccountLoading = false;
1086 this.state.deleteAccountShowConfirm = false;
1087 this.setState(this.state);
1088 this.context.router.history.push('/');