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 mt-1">
360 <table class="table table-bordered table-sm mt-2 mb-0">
362 <td class="text-center" colSpan={2}>
363 {i18n.t('number_of_points', {
364 count: user.post_score + user.comment_score,
370 {i18n.t('number_of_points', { count: user.post_score })}
373 {i18n.t('number_of_posts', { count: user.number_of_posts })}
378 {i18n.t('number_of_points', { count: user.comment_score })}
381 {i18n.t('number_of_comments', {
382 count: user.number_of_comments,
388 {this.isCurrentUser ? (
390 class="btn btn-block btn-secondary mt-3"
391 onClick={linkEvent(this, this.handleLogoutClick)}
398 className={`btn btn-block btn-secondary mt-3 ${!this.state
399 .user.matrix_user_id && 'disabled'}`}
401 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
403 {i18n.t('send_secure_message')}
406 class="btn btn-block btn-secondary mt-3"
407 to={`/create_private_message?recipient_id=${this.state.user.id}`}
409 {i18n.t('send_message')}
422 <div class="card border-secondary mb-3">
423 <div class="card-body">
424 <h5>{i18n.t('settings')}</h5>
425 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
426 <div class="form-group">
427 <label>{i18n.t('avatar')}</label>
428 <form class="d-inline">
430 htmlFor="file-upload"
431 class="pointer ml-4 text-muted small font-weight-bold"
433 {!this.state.userSettingsForm.avatar ? (
434 <span class="btn btn-sm btn-secondary">
435 {i18n.t('upload_avatar')}
441 src={this.state.userSettingsForm.avatar}
442 class="rounded-circle"
449 accept="image/*,video/*"
452 disabled={!UserService.Instance.user}
453 onChange={linkEvent(this, this.handleImageUpload)}
457 <div class="form-group">
458 <label>{i18n.t('language')}</label>
460 value={this.state.userSettingsForm.lang}
461 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
462 class="ml-2 custom-select custom-select-sm w-auto"
464 <option disabled>{i18n.t('language')}</option>
465 <option value="browser">{i18n.t('browser_default')}</option>
466 <option disabled>──</option>
467 {languages.map(lang => (
468 <option value={lang.code}>{lang.name}</option>
472 <div class="form-group">
473 <label>{i18n.t('theme')}</label>
475 value={this.state.userSettingsForm.theme}
476 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
477 class="ml-2 custom-select custom-select-sm w-auto"
479 <option disabled>{i18n.t('theme')}</option>
480 {themes.map(theme => (
481 <option value={theme}>{theme}</option>
485 <form className="form-group">
487 <div class="mr-2">{i18n.t('sort_type')}</div>
490 type_={this.state.userSettingsForm.default_listing_type}
491 onChange={this.handleUserSettingsListingTypeChange}
494 <form className="form-group">
496 <div class="mr-2">{i18n.t('type')}</div>
499 sort={this.state.userSettingsForm.default_sort_type}
500 onChange={this.handleUserSettingsSortTypeChange}
503 <div class="form-group row">
504 <label class="col-lg-3 col-form-label" htmlFor="user-email">
507 <div class="col-lg-9">
512 placeholder={i18n.t('optional')}
513 value={this.state.userSettingsForm.email}
516 this.handleUserSettingsEmailChange
522 <div class="form-group row">
523 <label class="col-lg-5 col-form-label">
524 <a href="https://about.riot.im/" target="_blank">
525 {i18n.t('matrix_user_id')}
528 <div class="col-lg-7">
532 placeholder="@user:example.com"
533 value={this.state.userSettingsForm.matrix_user_id}
536 this.handleUserSettingsMatrixUserIdChange
542 <div class="form-group row">
543 <label class="col-lg-5 col-form-label" htmlFor="user-password">
544 {i18n.t('new_password')}
546 <div class="col-lg-7">
551 value={this.state.userSettingsForm.new_password}
554 this.handleUserSettingsNewPasswordChange
559 <div class="form-group row">
561 class="col-lg-5 col-form-label"
562 htmlFor="user-verify-password"
564 {i18n.t('verify_password')}
566 <div class="col-lg-7">
569 id="user-verify-password"
571 value={this.state.userSettingsForm.new_password_verify}
574 this.handleUserSettingsNewPasswordVerifyChange
579 <div class="form-group row">
581 class="col-lg-5 col-form-label"
582 htmlFor="user-old-password"
584 {i18n.t('old_password')}
586 <div class="col-lg-7">
589 id="user-old-password"
591 value={this.state.userSettingsForm.old_password}
594 this.handleUserSettingsOldPasswordChange
599 {WebSocketService.Instance.site.enable_nsfw && (
600 <div class="form-group">
601 <div class="form-check">
603 class="form-check-input"
606 checked={this.state.userSettingsForm.show_nsfw}
609 this.handleUserSettingsShowNsfwChange
612 <label class="form-check-label" htmlFor="user-show-nsfw">
613 {i18n.t('show_nsfw')}
618 <div class="form-group">
619 <div class="form-check">
621 class="form-check-input"
622 id="user-show-avatars"
624 checked={this.state.userSettingsForm.show_avatars}
627 this.handleUserSettingsShowAvatarsChange
630 <label class="form-check-label" htmlFor="user-show-avatars">
631 {i18n.t('show_avatars')}
635 <div class="form-group">
636 <div class="form-check">
638 class="form-check-input"
639 id="user-send-notifications-to-email"
641 disabled={!this.state.user.email}
643 this.state.userSettingsForm.send_notifications_to_email
647 this.handleUserSettingsSendNotificationsToEmailChange
651 class="form-check-label"
652 htmlFor="user-send-notifications-to-email"
654 {i18n.t('send_notifications_to_email')}
658 <div class="form-group">
659 <button type="submit" class="btn btn-block btn-secondary mr-4">
660 {this.state.userSettingsLoading ? (
661 <svg class="icon icon-spinner spin">
662 <use xlinkHref="#icon-spinner"></use>
665 capitalizeFirstLetter(i18n.t('save'))
670 <div class="form-group mb-0">
672 class="btn btn-block btn-danger"
675 this.handleDeleteAccountShowConfirmToggle
678 {i18n.t('delete_account')}
680 {this.state.deleteAccountShowConfirm && (
682 <div class="my-2 alert alert-danger" role="alert">
683 {i18n.t('delete_account_confirm')}
687 value={this.state.deleteAccountForm.password}
690 this.handleDeleteAccountPasswordChange
692 class="form-control my-2"
695 class="btn btn-danger mr-4"
696 disabled={!this.state.deleteAccountForm.password}
697 onClick={linkEvent(this, this.handleDeleteAccount)}
699 {this.state.deleteAccountLoading ? (
700 <svg class="icon icon-spinner spin">
701 <use xlinkHref="#icon-spinner"></use>
704 capitalizeFirstLetter(i18n.t('delete'))
708 class="btn btn-secondary"
711 this.handleDeleteAccountShowConfirmToggle
729 {this.state.moderates.length > 0 && (
730 <div class="card border-secondary mb-3">
731 <div class="card-body">
732 <h5>{i18n.t('moderates')}</h5>
733 <ul class="list-unstyled mb-0">
734 {this.state.moderates.map(community => (
736 <Link to={`/c/${community.community_name}`}>
737 {community.community_name}
752 {this.state.follows.length > 0 && (
753 <div class="card border-secondary mb-3">
754 <div class="card-body">
755 <h5>{i18n.t('subscribed')}</h5>
756 <ul class="list-unstyled mb-0">
757 {this.state.follows.map(community => (
759 <Link to={`/c/${community.community_name}`}>
760 {community.community_name}
775 {this.state.page > 1 && (
777 class="btn btn-sm btn-secondary mr-1"
778 onClick={linkEvent(this, this.prevPage)}
784 class="btn btn-sm btn-secondary"
785 onClick={linkEvent(this, this.nextPage)}
794 let viewStr = View[this.state.view].toLowerCase();
795 let sortStr = SortType[this.state.sort].toLowerCase();
796 this.props.history.push(
797 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
816 let form: GetUserDetailsForm = {
817 user_id: this.state.user_id,
818 username: this.state.username,
819 sort: SortType[this.state.sort],
820 saved_only: this.state.view == View.Saved,
821 page: this.state.page,
824 WebSocketService.Instance.getUserDetails(form);
827 handleSortChange(val: SortType) {
828 this.state.sort = val;
830 this.setState(this.state);
835 handleViewChange(i: User, event: any) {
836 i.state.view = Number(event.target.value);
843 handleUserSettingsShowNsfwChange(i: User, event: any) {
844 i.state.userSettingsForm.show_nsfw = event.target.checked;
848 handleUserSettingsShowAvatarsChange(i: User, event: any) {
849 i.state.userSettingsForm.show_avatars = event.target.checked;
850 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
854 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
855 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
859 handleUserSettingsThemeChange(i: User, event: any) {
860 i.state.userSettingsForm.theme = event.target.value;
861 setTheme(event.target.value);
865 handleUserSettingsLangChange(i: User, event: any) {
866 i.state.userSettingsForm.lang = event.target.value;
867 i18n.changeLanguage(i.state.userSettingsForm.lang);
871 handleUserSettingsSortTypeChange(val: SortType) {
872 this.state.userSettingsForm.default_sort_type = val;
873 this.setState(this.state);
876 handleUserSettingsListingTypeChange(val: ListingType) {
877 this.state.userSettingsForm.default_listing_type = val;
878 this.setState(this.state);
881 handleUserSettingsEmailChange(i: User, event: any) {
882 i.state.userSettingsForm.email = event.target.value;
883 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
884 i.state.userSettingsForm.email = undefined;
889 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
890 i.state.userSettingsForm.matrix_user_id = event.target.value;
892 i.state.userSettingsForm.matrix_user_id == '' &&
893 !i.state.user.matrix_user_id
895 i.state.userSettingsForm.matrix_user_id = undefined;
900 handleUserSettingsNewPasswordChange(i: User, event: any) {
901 i.state.userSettingsForm.new_password = event.target.value;
902 if (i.state.userSettingsForm.new_password == '') {
903 i.state.userSettingsForm.new_password = undefined;
908 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
909 i.state.userSettingsForm.new_password_verify = event.target.value;
910 if (i.state.userSettingsForm.new_password_verify == '') {
911 i.state.userSettingsForm.new_password_verify = undefined;
916 handleUserSettingsOldPasswordChange(i: User, event: any) {
917 i.state.userSettingsForm.old_password = event.target.value;
918 if (i.state.userSettingsForm.old_password == '') {
919 i.state.userSettingsForm.old_password = undefined;
924 handleImageUpload(i: User, event: any) {
925 event.preventDefault();
926 let file = event.target.files[0];
927 const imageUploadUrl = `/pictshare/api/upload.php`;
928 const formData = new FormData();
929 formData.append('file', file);
931 i.state.avatarLoading = true;
934 fetch(imageUploadUrl, {
938 .then(res => res.json())
940 let url = `${window.location.origin}/pictshare/${res.url}`;
941 if (res.filetype == 'mp4') {
944 i.state.userSettingsForm.avatar = url;
946 i.state.avatarLoading = false;
950 i.state.avatarLoading = false;
952 toast(error, 'danger');
956 handleUserSettingsSubmit(i: User, event: any) {
957 event.preventDefault();
958 i.state.userSettingsLoading = true;
961 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
964 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
965 event.preventDefault();
966 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
970 handleDeleteAccountPasswordChange(i: User, event: any) {
971 i.state.deleteAccountForm.password = event.target.value;
975 handleLogoutClick(i: User) {
976 UserService.Instance.logout();
977 i.context.router.history.push('/');
980 handleDeleteAccount(i: User, event: any) {
981 event.preventDefault();
982 i.state.deleteAccountLoading = true;
985 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
988 parseMessage(msg: WebSocketJsonResponse) {
990 let res = wsJsonToRes(msg);
992 toast(i18n.t(msg.error), 'danger');
993 this.state.deleteAccountLoading = false;
994 this.state.avatarLoading = false;
995 this.state.userSettingsLoading = false;
996 if (msg.error == 'couldnt_find_that_username_or_email') {
997 this.context.router.history.push('/');
999 this.setState(this.state);
1001 } else if (msg.reconnect) {
1003 } else if (res.op == UserOperation.GetUserDetails) {
1004 let data = res.data as UserDetailsResponse;
1005 this.state.user = data.user;
1006 this.state.comments = data.comments;
1007 this.state.follows = data.follows;
1008 this.state.moderates = data.moderates;
1009 this.state.posts = data.posts;
1010 this.state.admins = data.admins;
1011 this.state.loading = false;
1012 if (this.isCurrentUser) {
1013 this.state.userSettingsForm.show_nsfw =
1014 UserService.Instance.user.show_nsfw;
1015 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1016 ? UserService.Instance.user.theme
1018 this.state.userSettingsForm.default_sort_type =
1019 UserService.Instance.user.default_sort_type;
1020 this.state.userSettingsForm.default_listing_type =
1021 UserService.Instance.user.default_listing_type;
1022 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1023 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1024 this.state.userSettingsForm.email = this.state.user.email;
1025 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1026 this.state.userSettingsForm.show_avatars =
1027 UserService.Instance.user.show_avatars;
1028 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1030 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1031 window.scrollTo(0, 0);
1032 this.setState(this.state);
1033 } else if (res.op == UserOperation.EditComment) {
1034 let data = res.data as CommentResponse;
1036 let found = this.state.comments.find(c => c.id == data.comment.id);
1038 found.content = data.comment.content;
1039 found.updated = data.comment.updated;
1040 found.removed = data.comment.removed;
1041 found.deleted = data.comment.deleted;
1042 found.upvotes = data.comment.upvotes;
1043 found.downvotes = data.comment.downvotes;
1044 found.score = data.comment.score;
1045 this.setState(this.state);
1047 } else if (res.op == UserOperation.CreateComment) {
1048 let data = res.data as CommentResponse;
1050 UserService.Instance.user &&
1051 data.comment.creator_id == UserService.Instance.user.id
1053 toast(i18n.t('reply_sent'));
1055 } else if (res.op == UserOperation.SaveComment) {
1056 let data = res.data as CommentResponse;
1057 let found = this.state.comments.find(c => c.id == data.comment.id);
1058 found.saved = data.comment.saved;
1059 this.setState(this.state);
1060 } else if (res.op == UserOperation.CreateCommentLike) {
1061 let data = res.data as CommentResponse;
1062 let found: Comment = this.state.comments.find(
1063 c => c.id === data.comment.id
1065 found.score = data.comment.score;
1066 found.upvotes = data.comment.upvotes;
1067 found.downvotes = data.comment.downvotes;
1068 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1069 this.setState(this.state);
1070 } else if (res.op == UserOperation.CreatePostLike) {
1071 let data = res.data as PostResponse;
1072 let found = this.state.posts.find(c => c.id == data.post.id);
1073 found.my_vote = data.post.my_vote;
1074 found.score = data.post.score;
1075 found.upvotes = data.post.upvotes;
1076 found.downvotes = data.post.downvotes;
1077 this.setState(this.state);
1078 } else if (res.op == UserOperation.BanUser) {
1079 let data = res.data as BanUserResponse;
1081 .filter(c => c.creator_id == data.user.id)
1082 .forEach(c => (c.banned = data.banned));
1084 .filter(c => c.creator_id == data.user.id)
1085 .forEach(c => (c.banned = data.banned));
1086 this.setState(this.state);
1087 } else if (res.op == UserOperation.AddAdmin) {
1088 let data = res.data as AddAdminResponse;
1089 this.state.admins = data.admins;
1090 this.setState(this.state);
1091 } else if (res.op == UserOperation.SaveUserSettings) {
1092 let data = res.data as LoginResponse;
1093 this.state = this.emptyState;
1094 this.state.userSettingsLoading = false;
1095 this.setState(this.state);
1096 UserService.Instance.login(data);
1097 } else if (res.op == UserOperation.DeleteAccount) {
1098 this.state.deleteAccountLoading = false;
1099 this.state.deleteAccountShowConfirm = false;
1100 this.setState(this.state);
1101 this.context.router.history.push('/');