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}
327 nodes={commentsToFlatNodes(this.state.comments)}
328 admins={this.state.admins}
338 {this.state.posts.map(post => (
339 <PostListing post={post} admins={this.state.admins} showCommunity />
346 let user = this.state.user;
349 <div class="card border-secondary mb-3">
350 <div class="card-body">
352 <ul class="list-inline mb-0">
353 <li className="list-inline-item">{user.name}</li>
355 <li className="list-inline-item badge badge-danger">
362 {i18n.t('joined')} <MomentTime data={user} showAgo />
364 <div class="table-responsive mt-1">
365 <table class="table table-bordered table-sm mt-2 mb-0">
368 <td class="text-center" colSpan={2}>
369 {i18n.t('number_of_points', {
370 count: user.post_score + user.comment_score,
378 {i18n.t('number_of_points', { count: user.post_score })}
382 {i18n.t('number_of_posts', { count: user.number_of_posts })}
388 {i18n.t('number_of_points', { count: user.comment_score })}
392 {i18n.t('number_of_comments', {
393 count: user.number_of_comments,
399 {this.isCurrentUser ? (
401 class="btn btn-block btn-secondary mt-3"
402 onClick={linkEvent(this, this.handleLogoutClick)}
409 className={`btn btn-block btn-secondary mt-3 ${!this.state
410 .user.matrix_user_id && 'disabled'}`}
412 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
414 {i18n.t('send_secure_message')}
417 class="btn btn-block btn-secondary mt-3"
418 to={`/create_private_message?recipient_id=${this.state.user.id}`}
420 {i18n.t('send_message')}
433 <div class="card border-secondary mb-3">
434 <div class="card-body">
435 <h5>{i18n.t('settings')}</h5>
436 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
437 <div class="form-group">
438 <label>{i18n.t('avatar')}</label>
439 <form class="d-inline">
441 htmlFor="file-upload"
442 class="pointer ml-4 text-muted small font-weight-bold"
444 {!this.state.userSettingsForm.avatar ? (
445 <span class="btn btn-sm btn-secondary">
446 {i18n.t('upload_avatar')}
452 src={this.state.userSettingsForm.avatar}
453 class="rounded-circle"
460 accept="image/*,video/*"
463 disabled={!UserService.Instance.user}
464 onChange={linkEvent(this, this.handleImageUpload)}
468 <div class="form-group">
469 <label>{i18n.t('language')}</label>
471 value={this.state.userSettingsForm.lang}
472 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
473 class="ml-2 custom-select custom-select-sm w-auto"
475 <option disabled>{i18n.t('language')}</option>
476 <option value="browser">{i18n.t('browser_default')}</option>
477 <option disabled>──</option>
478 {languages.map(lang => (
479 <option value={lang.code}>{lang.name}</option>
483 <div class="form-group">
484 <label>{i18n.t('theme')}</label>
486 value={this.state.userSettingsForm.theme}
487 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
488 class="ml-2 custom-select custom-select-sm w-auto"
490 <option disabled>{i18n.t('theme')}</option>
491 {themes.map(theme => (
492 <option value={theme}>{theme}</option>
496 <form className="form-group">
498 <div class="mr-2">{i18n.t('sort_type')}</div>
501 type_={this.state.userSettingsForm.default_listing_type}
502 onChange={this.handleUserSettingsListingTypeChange}
505 <form className="form-group">
507 <div class="mr-2">{i18n.t('type')}</div>
510 sort={this.state.userSettingsForm.default_sort_type}
511 onChange={this.handleUserSettingsSortTypeChange}
514 <div class="form-group row">
515 <label class="col-lg-3 col-form-label" htmlFor="user-email">
518 <div class="col-lg-9">
523 placeholder={i18n.t('optional')}
524 value={this.state.userSettingsForm.email}
527 this.handleUserSettingsEmailChange
533 <div class="form-group row">
534 <label class="col-lg-5 col-form-label">
535 <a href="https://about.riot.im/" target="_blank">
536 {i18n.t('matrix_user_id')}
539 <div class="col-lg-7">
543 placeholder="@user:example.com"
544 value={this.state.userSettingsForm.matrix_user_id}
547 this.handleUserSettingsMatrixUserIdChange
553 <div class="form-group row">
554 <label class="col-lg-5 col-form-label" htmlFor="user-password">
555 {i18n.t('new_password')}
557 <div class="col-lg-7">
562 value={this.state.userSettingsForm.new_password}
565 this.handleUserSettingsNewPasswordChange
570 <div class="form-group row">
572 class="col-lg-5 col-form-label"
573 htmlFor="user-verify-password"
575 {i18n.t('verify_password')}
577 <div class="col-lg-7">
580 id="user-verify-password"
582 value={this.state.userSettingsForm.new_password_verify}
585 this.handleUserSettingsNewPasswordVerifyChange
590 <div class="form-group row">
592 class="col-lg-5 col-form-label"
593 htmlFor="user-old-password"
595 {i18n.t('old_password')}
597 <div class="col-lg-7">
600 id="user-old-password"
602 value={this.state.userSettingsForm.old_password}
605 this.handleUserSettingsOldPasswordChange
610 {WebSocketService.Instance.site.enable_nsfw && (
611 <div class="form-group">
612 <div class="form-check">
614 class="form-check-input"
617 checked={this.state.userSettingsForm.show_nsfw}
620 this.handleUserSettingsShowNsfwChange
623 <label class="form-check-label" htmlFor="user-show-nsfw">
624 {i18n.t('show_nsfw')}
629 <div class="form-group">
630 <div class="form-check">
632 class="form-check-input"
633 id="user-show-avatars"
635 checked={this.state.userSettingsForm.show_avatars}
638 this.handleUserSettingsShowAvatarsChange
641 <label class="form-check-label" htmlFor="user-show-avatars">
642 {i18n.t('show_avatars')}
646 <div class="form-group">
647 <div class="form-check">
649 class="form-check-input"
650 id="user-send-notifications-to-email"
652 disabled={!this.state.user.email}
654 this.state.userSettingsForm.send_notifications_to_email
658 this.handleUserSettingsSendNotificationsToEmailChange
662 class="form-check-label"
663 htmlFor="user-send-notifications-to-email"
665 {i18n.t('send_notifications_to_email')}
669 <div class="form-group">
670 <button type="submit" class="btn btn-block btn-secondary mr-4">
671 {this.state.userSettingsLoading ? (
672 <svg class="icon icon-spinner spin">
673 <use xlinkHref="#icon-spinner"></use>
676 capitalizeFirstLetter(i18n.t('save'))
681 <div class="form-group mb-0">
683 class="btn btn-block btn-danger"
686 this.handleDeleteAccountShowConfirmToggle
689 {i18n.t('delete_account')}
691 {this.state.deleteAccountShowConfirm && (
693 <div class="my-2 alert alert-danger" role="alert">
694 {i18n.t('delete_account_confirm')}
698 value={this.state.deleteAccountForm.password}
701 this.handleDeleteAccountPasswordChange
703 class="form-control my-2"
706 class="btn btn-danger mr-4"
707 disabled={!this.state.deleteAccountForm.password}
708 onClick={linkEvent(this, this.handleDeleteAccount)}
710 {this.state.deleteAccountLoading ? (
711 <svg class="icon icon-spinner spin">
712 <use xlinkHref="#icon-spinner"></use>
715 capitalizeFirstLetter(i18n.t('delete'))
719 class="btn btn-secondary"
722 this.handleDeleteAccountShowConfirmToggle
740 {this.state.moderates.length > 0 && (
741 <div class="card border-secondary mb-3">
742 <div class="card-body">
743 <h5>{i18n.t('moderates')}</h5>
744 <ul class="list-unstyled mb-0">
745 {this.state.moderates.map(community => (
747 <Link to={`/c/${community.community_name}`}>
748 {community.community_name}
763 {this.state.follows.length > 0 && (
764 <div class="card border-secondary mb-3">
765 <div class="card-body">
766 <h5>{i18n.t('subscribed')}</h5>
767 <ul class="list-unstyled mb-0">
768 {this.state.follows.map(community => (
770 <Link to={`/c/${community.community_name}`}>
771 {community.community_name}
786 {this.state.page > 1 && (
788 class="btn btn-sm btn-secondary mr-1"
789 onClick={linkEvent(this, this.prevPage)}
795 class="btn btn-sm btn-secondary"
796 onClick={linkEvent(this, this.nextPage)}
805 let viewStr = View[this.state.view].toLowerCase();
806 let sortStr = SortType[this.state.sort].toLowerCase();
807 this.props.history.push(
808 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
827 let form: GetUserDetailsForm = {
828 user_id: this.state.user_id,
829 username: this.state.username,
830 sort: SortType[this.state.sort],
831 saved_only: this.state.view == View.Saved,
832 page: this.state.page,
835 WebSocketService.Instance.getUserDetails(form);
838 handleSortChange(val: SortType) {
839 this.state.sort = val;
841 this.setState(this.state);
846 handleViewChange(i: User, event: any) {
847 i.state.view = Number(event.target.value);
854 handleUserSettingsShowNsfwChange(i: User, event: any) {
855 i.state.userSettingsForm.show_nsfw = event.target.checked;
859 handleUserSettingsShowAvatarsChange(i: User, event: any) {
860 i.state.userSettingsForm.show_avatars = event.target.checked;
861 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
865 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
866 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
870 handleUserSettingsThemeChange(i: User, event: any) {
871 i.state.userSettingsForm.theme = event.target.value;
872 setTheme(event.target.value);
876 handleUserSettingsLangChange(i: User, event: any) {
877 i.state.userSettingsForm.lang = event.target.value;
878 i18n.changeLanguage(i.state.userSettingsForm.lang);
882 handleUserSettingsSortTypeChange(val: SortType) {
883 this.state.userSettingsForm.default_sort_type = val;
884 this.setState(this.state);
887 handleUserSettingsListingTypeChange(val: ListingType) {
888 this.state.userSettingsForm.default_listing_type = val;
889 this.setState(this.state);
892 handleUserSettingsEmailChange(i: User, event: any) {
893 i.state.userSettingsForm.email = event.target.value;
894 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
895 i.state.userSettingsForm.email = undefined;
900 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
901 i.state.userSettingsForm.matrix_user_id = event.target.value;
903 i.state.userSettingsForm.matrix_user_id == '' &&
904 !i.state.user.matrix_user_id
906 i.state.userSettingsForm.matrix_user_id = undefined;
911 handleUserSettingsNewPasswordChange(i: User, event: any) {
912 i.state.userSettingsForm.new_password = event.target.value;
913 if (i.state.userSettingsForm.new_password == '') {
914 i.state.userSettingsForm.new_password = undefined;
919 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
920 i.state.userSettingsForm.new_password_verify = event.target.value;
921 if (i.state.userSettingsForm.new_password_verify == '') {
922 i.state.userSettingsForm.new_password_verify = undefined;
927 handleUserSettingsOldPasswordChange(i: User, event: any) {
928 i.state.userSettingsForm.old_password = event.target.value;
929 if (i.state.userSettingsForm.old_password == '') {
930 i.state.userSettingsForm.old_password = undefined;
935 handleImageUpload(i: User, event: any) {
936 event.preventDefault();
937 let file = event.target.files[0];
938 const imageUploadUrl = `/pictshare/api/upload.php`;
939 const formData = new FormData();
940 formData.append('file', file);
942 i.state.avatarLoading = true;
945 fetch(imageUploadUrl, {
949 .then(res => res.json())
951 let url = `${window.location.origin}/pictshare/${res.url}`;
952 if (res.filetype == 'mp4') {
955 i.state.userSettingsForm.avatar = url;
957 i.state.avatarLoading = false;
961 i.state.avatarLoading = false;
963 toast(error, 'danger');
967 handleUserSettingsSubmit(i: User, event: any) {
968 event.preventDefault();
969 i.state.userSettingsLoading = true;
972 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
975 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
976 event.preventDefault();
977 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
981 handleDeleteAccountPasswordChange(i: User, event: any) {
982 i.state.deleteAccountForm.password = event.target.value;
986 handleLogoutClick(i: User) {
987 UserService.Instance.logout();
988 i.context.router.history.push('/');
991 handleDeleteAccount(i: User, event: any) {
992 event.preventDefault();
993 i.state.deleteAccountLoading = true;
996 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
999 parseMessage(msg: WebSocketJsonResponse) {
1001 let res = wsJsonToRes(msg);
1003 toast(i18n.t(msg.error), 'danger');
1004 this.state.deleteAccountLoading = false;
1005 this.state.avatarLoading = false;
1006 this.state.userSettingsLoading = false;
1007 if (msg.error == 'couldnt_find_that_username_or_email') {
1008 this.context.router.history.push('/');
1010 this.setState(this.state);
1012 } else if (msg.reconnect) {
1014 } else if (res.op == UserOperation.GetUserDetails) {
1015 let data = res.data as UserDetailsResponse;
1016 this.state.user = data.user;
1017 this.state.comments = data.comments;
1018 this.state.follows = data.follows;
1019 this.state.moderates = data.moderates;
1020 this.state.posts = data.posts;
1021 this.state.admins = data.admins;
1022 this.state.loading = false;
1023 if (this.isCurrentUser) {
1024 this.state.userSettingsForm.show_nsfw =
1025 UserService.Instance.user.show_nsfw;
1026 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1027 ? UserService.Instance.user.theme
1029 this.state.userSettingsForm.default_sort_type =
1030 UserService.Instance.user.default_sort_type;
1031 this.state.userSettingsForm.default_listing_type =
1032 UserService.Instance.user.default_listing_type;
1033 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1034 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1035 this.state.userSettingsForm.email = this.state.user.email;
1036 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1037 this.state.userSettingsForm.show_avatars =
1038 UserService.Instance.user.show_avatars;
1039 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1041 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1042 window.scrollTo(0, 0);
1043 this.setState(this.state);
1045 } else if (res.op == UserOperation.EditComment) {
1046 let data = res.data as CommentResponse;
1047 editCommentRes(data, this.state.comments);
1048 this.setState(this.state);
1049 } else if (res.op == UserOperation.CreateComment) {
1050 let data = res.data as CommentResponse;
1052 UserService.Instance.user &&
1053 data.comment.creator_id == UserService.Instance.user.id
1055 toast(i18n.t('reply_sent'));
1057 } else if (res.op == UserOperation.SaveComment) {
1058 let data = res.data as CommentResponse;
1059 saveCommentRes(data, this.state.comments);
1060 this.setState(this.state);
1061 } else if (res.op == UserOperation.CreateCommentLike) {
1062 let data = res.data as CommentResponse;
1063 createCommentLikeRes(data, this.state.comments);
1064 this.setState(this.state);
1065 } else if (res.op == UserOperation.CreatePostLike) {
1066 let data = res.data as PostResponse;
1067 createPostLikeFindRes(data, this.state.posts);
1068 this.setState(this.state);
1069 } else if (res.op == UserOperation.BanUser) {
1070 let data = res.data as BanUserResponse;
1072 .filter(c => c.creator_id == data.user.id)
1073 .forEach(c => (c.banned = data.banned));
1075 .filter(c => c.creator_id == data.user.id)
1076 .forEach(c => (c.banned = data.banned));
1077 this.setState(this.state);
1078 } else if (res.op == UserOperation.AddAdmin) {
1079 let data = res.data as AddAdminResponse;
1080 this.state.admins = data.admins;
1081 this.setState(this.state);
1082 } else if (res.op == UserOperation.SaveUserSettings) {
1083 let data = res.data as LoginResponse;
1084 this.state = this.emptyState;
1085 this.state.userSettingsLoading = false;
1086 this.setState(this.state);
1087 UserService.Instance.login(data);
1088 } else if (res.op == UserOperation.DeleteAccount) {
1089 this.state.deleteAccountLoading = false;
1090 this.state.deleteAccountShowConfirm = false;
1091 this.setState(this.state);
1092 this.context.router.history.push('/');