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';
21 CreatePostLikeResponse,
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';
42 import { T } from 'inferno-i18next';
55 follows: Array<CommunityUser>;
56 moderates: Array<CommunityUser>;
57 comments: Array<Comment>;
60 admins: Array<UserView>;
65 avatarLoading: boolean;
66 userSettingsForm: UserSettingsForm;
67 userSettingsLoading: boolean;
68 deleteAccountLoading: boolean;
69 deleteAccountShowConfirm: boolean;
70 deleteAccountForm: DeleteAccountForm;
73 export class User extends Component<any, UserState> {
74 private subscription: Subscription;
75 private emptyState: UserState = {
81 number_of_posts: null,
83 number_of_comments: null,
88 send_notifications_to_email: null,
99 view: this.getViewFromProps(this.props),
100 sort: this.getSortTypeFromProps(this.props),
101 page: this.getPageFromProps(this.props),
105 default_sort_type: null,
106 default_listing_type: null,
109 send_notifications_to_email: null,
112 userSettingsLoading: null,
113 deleteAccountLoading: null,
114 deleteAccountShowConfirm: false,
120 constructor(props: any, context: any) {
121 super(props, context);
123 this.state = this.emptyState;
124 this.handleSortChange = this.handleSortChange.bind(this);
125 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
128 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
132 this.state.user_id = Number(this.props.match.params.id);
133 this.state.username = this.props.match.params.username;
135 this.subscription = WebSocketService.Instance.subject
136 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
138 msg => this.parseMessage(msg),
139 err => console.error(err),
140 () => console.log('complete')
146 get isCurrentUser() {
148 UserService.Instance.user &&
149 UserService.Instance.user.id == this.state.user.id
153 getViewFromProps(props: any): View {
154 return props.match.params.view
155 ? View[capitalizeFirstLetter(props.match.params.view)]
159 getSortTypeFromProps(props: any): SortType {
160 return props.match.params.sort
161 ? routeSortTypeToEnum(props.match.params.sort)
165 getPageFromProps(props: any): number {
166 return props.match.params.page ? Number(props.match.params.page) : 1;
169 componentWillUnmount() {
170 this.subscription.unsubscribe();
173 // Necessary for back button for some reason
174 componentWillReceiveProps(nextProps: any) {
176 nextProps.history.action == 'POP' ||
177 nextProps.history.action == 'PUSH'
179 this.state.view = this.getViewFromProps(nextProps);
180 this.state.sort = this.getSortTypeFromProps(nextProps);
181 this.state.page = this.getPageFromProps(nextProps);
182 this.setState(this.state);
187 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
188 // Necessary if you are on a post and you click another post (same route)
190 lastProps.location.pathname.split('/')[2] !==
191 lastProps.history.location.pathname.split('/')[2]
193 // Couldnt get a refresh working. This does for now.
200 <div class="container">
201 {this.state.loading ? (
202 <p class="text-center">
203 <svg class="icon icon-spinner spin">
204 <use xlinkHref="#icon-spinner"></use>
209 <div class="col-12 col-md-8">
211 {this.state.user.avatar && showAvatars() && (
215 src={this.state.user.avatar}
216 class="rounded-circle mr-2"
219 <span>/u/{this.state.user.name}</span>
222 {this.state.view == View.Overview && this.overview()}
223 {this.state.view == View.Comments && this.comments()}
224 {this.state.view == View.Posts && this.posts()}
225 {this.state.view == View.Saved && this.overview()}
228 <div class="col-12 col-md-4">
230 {this.isCurrentUser && this.userSettings()}
242 <div className="mb-2">
244 value={this.state.view}
245 onChange={linkEvent(this, this.handleViewChange)}
246 class="custom-select custom-select-sm w-auto"
251 <option value={View.Overview}>
252 { i18n.t('overview') }
254 <option value={View.Comments}>
255 { i18n.t('comments') }
257 <option value={View.Posts}>
260 <option value={View.Saved}>
266 sort={this.state.sort}
267 onChange={this.handleSortChange}
272 href={`/feeds/u/${this.state.username}.xml?sort=${
273 SortType[this.state.sort]
277 <svg class="icon mx-2 text-muted small">
278 <use xlinkHref="#icon-rss">#</use>
286 let combined: Array<{ type_: string; data: Comment | Post }> = [];
287 let comments = this.state.comments.map(e => {
288 return { type_: 'comments', data: e };
290 let posts = this.state.posts.map(e => {
291 return { type_: 'posts', data: e };
294 combined.push(...comments);
295 combined.push(...posts);
298 if (this.state.sort == SortType.New) {
299 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
301 combined.sort((a, b) => b.data.score - a.data.score);
308 {i.type_ == 'posts' ? (
310 post={i.data as Post}
311 admins={this.state.admins}
316 nodes={[{ comment: i.data as Comment }]}
317 admins={this.state.admins}
330 {this.state.comments.map(comment => (
332 nodes={[{ comment: comment }]}
333 admins={this.state.admins}
344 {this.state.posts.map(post => (
345 <PostListing post={post} admins={this.state.admins} showCommunity />
352 let user = this.state.user;
355 <div class="card border-secondary mb-3">
356 <div class="card-body">
358 <ul class="list-inline mb-0">
359 <li className="list-inline-item">{user.name}</li>
361 <li className="list-inline-item badge badge-danger">
368 {i18n.t('joined')} <MomentTime data={user} />
370 <div class="table-responsive">
371 <table class="table table-bordered table-sm mt-2 mb-0">
375 i18nKey="number_of_points"
376 interpolation={{ count: user.post_score }}
383 i18nKey="number_of_posts"
384 interpolation={{ count: user.number_of_posts }}
393 i18nKey="number_of_points"
394 interpolation={{ count: user.comment_score }}
401 i18nKey="number_of_comments"
402 interpolation={{ count: user.number_of_comments }}
410 {this.isCurrentUser ? (
412 class="btn btn-block btn-secondary mt-3"
413 onClick={linkEvent(this, this.handleLogoutClick)}
420 className={`btn btn-block btn-secondary mt-3 ${!this.state
421 .user.matrix_user_id && 'disabled'}`}
423 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
425 {i18n.t('send_secure_message')}
428 class="btn btn-block btn-secondary mt-3"
429 to={`/create_private_message?recipient_id=${this.state.user.id}`}
431 {i18n.t('send_message')}
444 <div class="card border-secondary mb-3">
445 <div class="card-body">
447 { i18n.t('settings') }
449 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
450 <div class="form-group">
454 <form class="d-inline">
456 htmlFor="file-upload"
457 class="pointer ml-4 text-muted small font-weight-bold"
459 {!this.state.userSettingsForm.avatar ? (
460 <span class="btn btn-sm btn-secondary">
461 { i18n.t('upload_avatar') }
467 src={this.state.userSettingsForm.avatar}
468 class="rounded-circle"
475 accept="image/*,video/*"
478 disabled={!UserService.Instance.user}
479 onChange={linkEvent(this, this.handleImageUpload)}
483 <div class="form-group">
485 { i18n.t('language') }
488 value={this.state.userSettingsForm.lang}
489 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
490 class="ml-2 custom-select custom-select-sm w-auto"
493 { i18n.t('language') }
495 <option value="browser">
496 { i18n.t('browser_default') }
498 <option disabled>──</option>
499 {languages.map(lang => (
500 <option value={lang.code}>{lang.name}</option>
504 <div class="form-group">
509 value={this.state.userSettingsForm.theme}
510 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
511 class="ml-2 custom-select custom-select-sm w-auto"
516 {themes.map(theme => (
517 <option value={theme}>{theme}</option>
521 <form className="form-group">
523 <T i18nKey="sort_type" class="mr-2">
528 type_={this.state.userSettingsForm.default_listing_type}
529 onChange={this.handleUserSettingsListingTypeChange}
532 <form className="form-group">
534 <T i18nKey="type" class="mr-2">
539 sort={this.state.userSettingsForm.default_sort_type}
540 onChange={this.handleUserSettingsSortTypeChange}
543 <div class="form-group row">
544 <label class="col-lg-3 col-form-label" for="user-email">
547 <div class="col-lg-9">
552 placeholder={i18n.t('optional')}
553 value={this.state.userSettingsForm.email}
556 this.handleUserSettingsEmailChange
562 <div class="form-group row">
563 <label class="col-lg-5 col-form-label">
564 <a href="https://about.riot.im/" target="_blank">
565 {i18n.t('matrix_user_id')}
568 <div class="col-lg-7">
572 placeholder="@user:example.com"
573 value={this.state.userSettingsForm.matrix_user_id}
576 this.handleUserSettingsMatrixUserIdChange
582 <div class="form-group row">
583 <label class="col-lg-5 col-form-label" for="user-password">
584 { i18n.t('new_password') }
586 <div class="col-lg-7">
591 value={this.state.userSettingsForm.new_password}
594 this.handleUserSettingsNewPasswordChange
599 <div class="form-group row">
600 <label class="col-lg-5 col-form-label" for="user-verify-password">
601 { i18n.t('verify_password') }
603 <div class="col-lg-7">
606 id="user-verify-password"
608 value={this.state.userSettingsForm.new_password_verify}
611 this.handleUserSettingsNewPasswordVerifyChange
616 <div class="form-group row">
617 <label class="col-lg-5 col-form-label" for="user-old-password">
618 { i18n.t('old_password') }
620 <div class="col-lg-7">
623 id="user-old-password"
625 value={this.state.userSettingsForm.old_password}
628 this.handleUserSettingsOldPasswordChange
633 {WebSocketService.Instance.site.enable_nsfw && (
634 <div class="form-group">
635 <div class="form-check">
637 class="form-check-input"
640 checked={this.state.userSettingsForm.show_nsfw}
643 this.handleUserSettingsShowNsfwChange
646 <label class="form-check-label" for="user-show-nsfw">
647 { i18n.t('show_nsfw') }
652 <div class="form-group">
653 <div class="form-check">
655 class="form-check-input"
656 id="user-show-avatars"
658 checked={this.state.userSettingsForm.show_avatars}
661 this.handleUserSettingsShowAvatarsChange
664 <label class="form-check-label" for="user-show-avatars">
665 { i18n.t('show_avatars') }
669 <div class="form-group">
670 <div class="form-check">
672 class="form-check-input"
673 id="user-send-notifications-to-email"
675 disabled={!this.state.user.email}
677 this.state.userSettingsForm.send_notifications_to_email
681 this.handleUserSettingsSendNotificationsToEmailChange
684 <label class="form-check-label" for="user-send-notifications-to-email">
685 { i18n.t('send_notifications_to_email') }
689 <div class="form-group">
690 <button type="submit" class="btn btn-block btn-secondary mr-4">
691 {this.state.userSettingsLoading ? (
692 <svg class="icon icon-spinner spin">
693 <use xlinkHref="#icon-spinner"></use>
696 capitalizeFirstLetter(i18n.t('save'))
701 <div class="form-group mb-0">
703 class="btn btn-block btn-danger"
706 this.handleDeleteAccountShowConfirmToggle
709 { i18n.t('delete_account') }
711 {this.state.deleteAccountShowConfirm && (
713 <div class="my-2 alert alert-danger" role="alert">
714 { i18n.t('delete_account_confirm') }
718 value={this.state.deleteAccountForm.password}
721 this.handleDeleteAccountPasswordChange
723 class="form-control my-2"
726 class="btn btn-danger mr-4"
727 disabled={!this.state.deleteAccountForm.password}
728 onClick={linkEvent(this, this.handleDeleteAccount)}
730 {this.state.deleteAccountLoading ? (
731 <svg class="icon icon-spinner spin">
732 <use xlinkHref="#icon-spinner"></use>
735 capitalizeFirstLetter(i18n.t('delete'))
739 class="btn btn-secondary"
742 this.handleDeleteAccountShowConfirmToggle
760 {this.state.moderates.length > 0 && (
761 <div class="card border-secondary mb-3">
762 <div class="card-body">
764 { i18n.t('moderates') }
766 <ul class="list-unstyled mb-0">
767 {this.state.moderates.map(community => (
769 <Link to={`/c/${community.community_name}`}>
770 {community.community_name}
785 {this.state.follows.length > 0 && (
786 <div class="card border-secondary mb-3">
787 <div class="card-body">
789 { i18n.t('subscribed') }
791 <ul class="list-unstyled mb-0">
792 {this.state.follows.map(community => (
794 <Link to={`/c/${community.community_name}`}>
795 {community.community_name}
810 {this.state.page > 1 && (
812 class="btn btn-sm btn-secondary mr-1"
813 onClick={linkEvent(this, this.prevPage)}
815 <T i18nKey="prev">#</T>
819 class="btn btn-sm btn-secondary"
820 onClick={linkEvent(this, this.nextPage)}
829 let viewStr = View[this.state.view].toLowerCase();
830 let sortStr = SortType[this.state.sort].toLowerCase();
831 this.props.history.push(
832 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
851 let form: GetUserDetailsForm = {
852 user_id: this.state.user_id,
853 username: this.state.username,
854 sort: SortType[this.state.sort],
855 saved_only: this.state.view == View.Saved,
856 page: this.state.page,
859 WebSocketService.Instance.getUserDetails(form);
862 handleSortChange(val: SortType) {
863 this.state.sort = val;
865 this.setState(this.state);
870 handleViewChange(i: User, event: any) {
871 i.state.view = Number(event.target.value);
878 handleUserSettingsShowNsfwChange(i: User, event: any) {
879 i.state.userSettingsForm.show_nsfw = event.target.checked;
883 handleUserSettingsShowAvatarsChange(i: User, event: any) {
884 i.state.userSettingsForm.show_avatars = event.target.checked;
885 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
889 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
890 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
894 handleUserSettingsThemeChange(i: User, event: any) {
895 i.state.userSettingsForm.theme = event.target.value;
896 setTheme(event.target.value);
900 handleUserSettingsLangChange(i: User, event: any) {
901 i.state.userSettingsForm.lang = event.target.value;
902 i18n.changeLanguage(i.state.userSettingsForm.lang);
906 handleUserSettingsSortTypeChange(val: SortType) {
907 this.state.userSettingsForm.default_sort_type = val;
908 this.setState(this.state);
911 handleUserSettingsListingTypeChange(val: ListingType) {
912 this.state.userSettingsForm.default_listing_type = val;
913 this.setState(this.state);
916 handleUserSettingsEmailChange(i: User, event: any) {
917 i.state.userSettingsForm.email = event.target.value;
918 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
919 i.state.userSettingsForm.email = undefined;
924 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
925 i.state.userSettingsForm.matrix_user_id = event.target.value;
927 i.state.userSettingsForm.matrix_user_id == '' &&
928 !i.state.user.matrix_user_id
930 i.state.userSettingsForm.matrix_user_id = undefined;
935 handleUserSettingsNewPasswordChange(i: User, event: any) {
936 i.state.userSettingsForm.new_password = event.target.value;
937 if (i.state.userSettingsForm.new_password == '') {
938 i.state.userSettingsForm.new_password = undefined;
943 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
944 i.state.userSettingsForm.new_password_verify = event.target.value;
945 if (i.state.userSettingsForm.new_password_verify == '') {
946 i.state.userSettingsForm.new_password_verify = undefined;
951 handleUserSettingsOldPasswordChange(i: User, event: any) {
952 i.state.userSettingsForm.old_password = event.target.value;
953 if (i.state.userSettingsForm.old_password == '') {
954 i.state.userSettingsForm.old_password = undefined;
959 handleImageUpload(i: User, event: any) {
960 event.preventDefault();
961 let file = event.target.files[0];
962 const imageUploadUrl = `/pictshare/api/upload.php`;
963 const formData = new FormData();
964 formData.append('file', file);
966 i.state.avatarLoading = true;
969 fetch(imageUploadUrl, {
973 .then(res => res.json())
975 let url = `${window.location.origin}/pictshare/${res.url}`;
976 if (res.filetype == 'mp4') {
979 i.state.userSettingsForm.avatar = url;
981 i.state.avatarLoading = false;
985 i.state.avatarLoading = false;
987 toast(error, 'danger');
991 handleUserSettingsSubmit(i: User, event: any) {
992 event.preventDefault();
993 i.state.userSettingsLoading = true;
996 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
999 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
1000 event.preventDefault();
1001 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1002 i.setState(i.state);
1005 handleDeleteAccountPasswordChange(i: User, event: any) {
1006 i.state.deleteAccountForm.password = event.target.value;
1007 i.setState(i.state);
1010 handleLogoutClick(i: User) {
1011 UserService.Instance.logout();
1012 i.context.router.history.push('/');
1015 handleDeleteAccount(i: User, event: any) {
1016 event.preventDefault();
1017 i.state.deleteAccountLoading = true;
1018 i.setState(i.state);
1020 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1023 parseMessage(msg: WebSocketJsonResponse) {
1025 let res = wsJsonToRes(msg);
1027 toast(i18n.t(msg.error), 'danger');
1028 this.state.deleteAccountLoading = false;
1029 this.state.avatarLoading = false;
1030 this.state.userSettingsLoading = false;
1031 if (msg.error == 'couldnt_find_that_username_or_email') {
1032 this.context.router.history.push('/');
1034 this.setState(this.state);
1036 } else if (res.op == UserOperation.GetUserDetails) {
1037 let data = res.data as UserDetailsResponse;
1038 this.state.user = data.user;
1039 this.state.comments = data.comments;
1040 this.state.follows = data.follows;
1041 this.state.moderates = data.moderates;
1042 this.state.posts = data.posts;
1043 this.state.admins = data.admins;
1044 this.state.loading = false;
1045 if (this.isCurrentUser) {
1046 this.state.userSettingsForm.show_nsfw =
1047 UserService.Instance.user.show_nsfw;
1048 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1049 ? UserService.Instance.user.theme
1051 this.state.userSettingsForm.default_sort_type =
1052 UserService.Instance.user.default_sort_type;
1053 this.state.userSettingsForm.default_listing_type =
1054 UserService.Instance.user.default_listing_type;
1055 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1056 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1057 this.state.userSettingsForm.email = this.state.user.email;
1058 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1059 this.state.userSettingsForm.show_avatars =
1060 UserService.Instance.user.show_avatars;
1061 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1063 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1064 window.scrollTo(0, 0);
1065 this.setState(this.state);
1066 } else if (res.op == UserOperation.EditComment) {
1067 let data = res.data as CommentResponse;
1069 let found = this.state.comments.find(c => c.id == data.comment.id);
1070 found.content = data.comment.content;
1071 found.updated = data.comment.updated;
1072 found.removed = data.comment.removed;
1073 found.deleted = data.comment.deleted;
1074 found.upvotes = data.comment.upvotes;
1075 found.downvotes = data.comment.downvotes;
1076 found.score = data.comment.score;
1078 this.setState(this.state);
1079 } else if (res.op == UserOperation.CreateComment) {
1080 // let res: CommentResponse = msg;
1081 toast(i18n.t('reply_sent'));
1082 // this.state.comments.unshift(res.comment); // TODO do this right
1083 // this.setState(this.state);
1084 } else if (res.op == UserOperation.SaveComment) {
1085 let data = res.data as CommentResponse;
1086 let found = this.state.comments.find(c => c.id == data.comment.id);
1087 found.saved = data.comment.saved;
1088 this.setState(this.state);
1089 } else if (res.op == UserOperation.CreateCommentLike) {
1090 let data = res.data as CommentResponse;
1091 let found: Comment = this.state.comments.find(
1092 c => c.id === data.comment.id
1094 found.score = data.comment.score;
1095 found.upvotes = data.comment.upvotes;
1096 found.downvotes = data.comment.downvotes;
1097 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1098 this.setState(this.state);
1099 } else if (res.op == UserOperation.CreatePostLike) {
1100 let data = res.data as CreatePostLikeResponse;
1101 let found = this.state.posts.find(c => c.id == data.post.id);
1102 found.my_vote = data.post.my_vote;
1103 found.score = data.post.score;
1104 found.upvotes = data.post.upvotes;
1105 found.downvotes = data.post.downvotes;
1106 this.setState(this.state);
1107 } else if (res.op == UserOperation.BanUser) {
1108 let data = res.data as BanUserResponse;
1110 .filter(c => c.creator_id == data.user.id)
1111 .forEach(c => (c.banned = data.banned));
1113 .filter(c => c.creator_id == data.user.id)
1114 .forEach(c => (c.banned = data.banned));
1115 this.setState(this.state);
1116 } else if (res.op == UserOperation.AddAdmin) {
1117 let data = res.data as AddAdminResponse;
1118 this.state.admins = data.admins;
1119 this.setState(this.state);
1120 } else if (res.op == UserOperation.SaveUserSettings) {
1121 let data = res.data as LoginResponse;
1122 this.state = this.emptyState;
1123 this.state.userSettingsLoading = false;
1124 this.setState(this.state);
1125 UserService.Instance.login(data);
1126 } else if (res.op == UserOperation.DeleteAccount) {
1127 this.state.deleteAccountLoading = false;
1128 this.state.deleteAccountShowConfirm = false;
1129 this.setState(this.state);
1130 this.context.router.history.push('/');