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 } from '../interfaces';
23 import { WebSocketService, UserService } from '../services';
28 capitalizeFirstLetter,
35 import { PostListing } from './post-listing';
36 import { SortSelect } from './sort-select';
37 import { ListingTypeSelect } from './listing-type-select';
38 import { CommentNodes } from './comment-nodes';
39 import { MomentTime } from './moment-time';
40 import { i18n } from '../i18next';
41 import { T } from 'inferno-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"
248 <T i18nKey="view">#</T>
250 <option value={View.Overview}>
251 <T i18nKey="overview">#</T>
253 <option value={View.Comments}>
254 <T i18nKey="comments">#</T>
256 <option value={View.Posts}>
257 <T i18nKey="posts">#</T>
259 <option value={View.Saved}>
260 <T i18nKey="saved">#</T>
265 sort={this.state.sort}
266 onChange={this.handleSortChange}
271 href={`/feeds/u/${this.state.username}.xml?sort=${
272 SortType[this.state.sort]
276 <svg class="icon mx-2 text-muted small">
277 <use xlinkHref="#icon-rss">#</use>
285 let combined: Array<{ type_: string; data: Comment | Post }> = [];
286 let comments = this.state.comments.map(e => {
287 return { type_: 'comments', data: e };
289 let posts = this.state.posts.map(e => {
290 return { type_: 'posts', data: e };
293 combined.push(...comments);
294 combined.push(...posts);
297 if (this.state.sort == SortType.New) {
298 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
300 combined.sort((a, b) => b.data.score - a.data.score);
307 {i.type_ == 'posts' ? (
309 post={i.data as Post}
310 admins={this.state.admins}
315 nodes={[{ comment: i.data as Comment }]}
316 admins={this.state.admins}
329 {this.state.comments.map(comment => (
331 nodes={[{ comment: comment }]}
332 admins={this.state.admins}
343 {this.state.posts.map(post => (
344 <PostListing post={post} admins={this.state.admins} showCommunity />
351 let user = this.state.user;
354 <div class="card border-secondary mb-3">
355 <div class="card-body">
357 <ul class="list-inline mb-0">
358 <li className="list-inline-item">{user.name}</li>
360 <li className="list-inline-item badge badge-danger">
361 <T i18nKey="banned">#</T>
367 {i18n.t('joined')} <MomentTime data={user} />
369 <div class="table-responsive">
370 <table class="table table-bordered table-sm mt-2 mb-0">
374 i18nKey="number_of_points"
375 interpolation={{ count: user.post_score }}
382 i18nKey="number_of_posts"
383 interpolation={{ count: user.number_of_posts }}
392 i18nKey="number_of_points"
393 interpolation={{ count: user.comment_score }}
400 i18nKey="number_of_comments"
401 interpolation={{ count: user.number_of_comments }}
409 {this.isCurrentUser ? (
411 class="btn btn-block btn-secondary mt-3"
412 onClick={linkEvent(this, this.handleLogoutClick)}
414 <T i18nKey="logout">#</T>
419 className={`btn btn-block btn-secondary mt-3 ${!this.state
420 .user.matrix_user_id && 'disabled'}`}
422 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
424 {i18n.t('send_secure_message')}
427 class="btn btn-block btn-secondary mt-3"
428 to={`/create_private_message?recipient_id=${this.state.user.id}`}
430 {i18n.t('send_message')}
443 <div class="card border-secondary mb-3">
444 <div class="card-body">
446 <T i18nKey="settings">#</T>
448 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
449 <div class="form-group">
451 <T i18nKey="avatar">#</T>
453 <form class="d-inline">
455 htmlFor="file-upload"
456 class="pointer ml-4 text-muted small font-weight-bold"
458 {!this.state.userSettingsForm.avatar ? (
459 <span class="btn btn-sm btn-secondary">
460 <T i18nKey="upload_avatar">#</T>
466 src={this.state.userSettingsForm.avatar}
467 class="rounded-circle"
474 accept="image/*,video/*"
477 disabled={!UserService.Instance.user}
478 onChange={linkEvent(this, this.handleImageUpload)}
482 <div class="form-group">
484 <T i18nKey="language">#</T>
487 value={this.state.userSettingsForm.lang}
488 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
489 class="ml-2 custom-select custom-select-sm w-auto"
492 <T i18nKey="language">#</T>
494 <option value="browser">
495 <T i18nKey="browser_default">#</T>
497 <option disabled>──</option>
498 {languages.map(lang => (
499 <option value={lang.code}>{lang.name}</option>
503 <div class="form-group">
505 <T i18nKey="theme">#</T>
508 value={this.state.userSettingsForm.theme}
509 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
510 class="ml-2 custom-select custom-select-sm w-auto"
513 <T i18nKey="theme">#</T>
515 {themes.map(theme => (
516 <option value={theme}>{theme}</option>
520 <form className="form-group">
522 <T i18nKey="sort_type" class="mr-2">
527 type_={this.state.userSettingsForm.default_listing_type}
528 onChange={this.handleUserSettingsListingTypeChange}
531 <form className="form-group">
533 <T i18nKey="type" class="mr-2">
538 sort={this.state.userSettingsForm.default_sort_type}
539 onChange={this.handleUserSettingsSortTypeChange}
542 <div class="form-group row">
543 <label class="col-lg-3 col-form-label">
544 <T i18nKey="email">#</T>
546 <div class="col-lg-9">
550 placeholder={i18n.t('optional')}
551 value={this.state.userSettingsForm.email}
554 this.handleUserSettingsEmailChange
560 <div class="form-group row">
561 <label class="col-lg-5 col-form-label">
562 <a href="https://about.riot.im/" target="_blank">
563 {i18n.t('matrix_user_id')}
566 <div class="col-lg-7">
570 placeholder="@user:example.com"
571 value={this.state.userSettingsForm.matrix_user_id}
574 this.handleUserSettingsMatrixUserIdChange
580 <div class="form-group row">
581 <label class="col-lg-5 col-form-label">
582 <T i18nKey="new_password">#</T>
584 <div class="col-lg-7">
588 value={this.state.userSettingsForm.new_password}
591 this.handleUserSettingsNewPasswordChange
596 <div class="form-group row">
597 <label class="col-lg-5 col-form-label">
598 <T i18nKey="verify_password">#</T>
600 <div class="col-lg-7">
604 value={this.state.userSettingsForm.new_password_verify}
607 this.handleUserSettingsNewPasswordVerifyChange
612 <div class="form-group row">
613 <label class="col-lg-5 col-form-label">
614 <T i18nKey="old_password">#</T>
616 <div class="col-lg-7">
620 value={this.state.userSettingsForm.old_password}
623 this.handleUserSettingsOldPasswordChange
628 {WebSocketService.Instance.site.enable_nsfw && (
629 <div class="form-group">
630 <div class="form-check">
632 class="form-check-input"
634 checked={this.state.userSettingsForm.show_nsfw}
637 this.handleUserSettingsShowNsfwChange
640 <label class="form-check-label">
641 <T i18nKey="show_nsfw">#</T>
646 <div class="form-group">
647 <div class="form-check">
649 class="form-check-input"
651 checked={this.state.userSettingsForm.show_avatars}
654 this.handleUserSettingsShowAvatarsChange
657 <label class="form-check-label">
658 <T i18nKey="show_avatars">#</T>
662 <div class="form-group">
663 <div class="form-check">
665 class="form-check-input"
667 disabled={!this.state.user.email}
669 this.state.userSettingsForm.send_notifications_to_email
673 this.handleUserSettingsSendNotificationsToEmailChange
676 <label class="form-check-label">
677 <T i18nKey="send_notifications_to_email">#</T>
681 <div class="form-group">
682 <button type="submit" class="btn btn-block btn-secondary mr-4">
683 {this.state.userSettingsLoading ? (
684 <svg class="icon icon-spinner spin">
685 <use xlinkHref="#icon-spinner"></use>
688 capitalizeFirstLetter(i18n.t('save'))
693 <div class="form-group mb-0">
695 class="btn btn-block btn-danger"
698 this.handleDeleteAccountShowConfirmToggle
701 <T i18nKey="delete_account">#</T>
703 {this.state.deleteAccountShowConfirm && (
705 <div class="my-2 alert alert-danger" role="alert">
706 <T i18nKey="delete_account_confirm">#</T>
710 value={this.state.deleteAccountForm.password}
713 this.handleDeleteAccountPasswordChange
715 class="form-control my-2"
718 class="btn btn-danger mr-4"
719 disabled={!this.state.deleteAccountForm.password}
720 onClick={linkEvent(this, this.handleDeleteAccount)}
722 {this.state.deleteAccountLoading ? (
723 <svg class="icon icon-spinner spin">
724 <use xlinkHref="#icon-spinner"></use>
727 capitalizeFirstLetter(i18n.t('delete'))
731 class="btn btn-secondary"
734 this.handleDeleteAccountShowConfirmToggle
737 <T i18nKey="cancel">#</T>
752 {this.state.moderates.length > 0 && (
753 <div class="card border-secondary mb-3">
754 <div class="card-body">
756 <T i18nKey="moderates">#</T>
758 <ul class="list-unstyled mb-0">
759 {this.state.moderates.map(community => (
761 <Link to={`/c/${community.community_name}`}>
762 {community.community_name}
777 {this.state.follows.length > 0 && (
778 <div class="card border-secondary mb-3">
779 <div class="card-body">
781 <T i18nKey="subscribed">#</T>
783 <ul class="list-unstyled mb-0">
784 {this.state.follows.map(community => (
786 <Link to={`/c/${community.community_name}`}>
787 {community.community_name}
802 {this.state.page > 1 && (
804 class="btn btn-sm btn-secondary mr-1"
805 onClick={linkEvent(this, this.prevPage)}
807 <T i18nKey="prev">#</T>
811 class="btn btn-sm btn-secondary"
812 onClick={linkEvent(this, this.nextPage)}
814 <T i18nKey="next">#</T>
821 let viewStr = View[this.state.view].toLowerCase();
822 let sortStr = SortType[this.state.sort].toLowerCase();
823 this.props.history.push(
824 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
843 let form: GetUserDetailsForm = {
844 user_id: this.state.user_id,
845 username: this.state.username,
846 sort: SortType[this.state.sort],
847 saved_only: this.state.view == View.Saved,
848 page: this.state.page,
851 WebSocketService.Instance.getUserDetails(form);
854 handleSortChange(val: SortType) {
855 this.state.sort = val;
857 this.setState(this.state);
862 handleViewChange(i: User, event: any) {
863 i.state.view = Number(event.target.value);
870 handleUserSettingsShowNsfwChange(i: User, event: any) {
871 i.state.userSettingsForm.show_nsfw = event.target.checked;
875 handleUserSettingsShowAvatarsChange(i: User, event: any) {
876 i.state.userSettingsForm.show_avatars = event.target.checked;
877 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
881 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
882 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
886 handleUserSettingsThemeChange(i: User, event: any) {
887 i.state.userSettingsForm.theme = event.target.value;
888 setTheme(event.target.value);
892 handleUserSettingsLangChange(i: User, event: any) {
893 i.state.userSettingsForm.lang = event.target.value;
894 i18n.changeLanguage(i.state.userSettingsForm.lang);
898 handleUserSettingsSortTypeChange(val: SortType) {
899 this.state.userSettingsForm.default_sort_type = val;
900 this.setState(this.state);
903 handleUserSettingsListingTypeChange(val: ListingType) {
904 this.state.userSettingsForm.default_listing_type = val;
905 this.setState(this.state);
908 handleUserSettingsEmailChange(i: User, event: any) {
909 i.state.userSettingsForm.email = event.target.value;
910 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
911 i.state.userSettingsForm.email = undefined;
916 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
917 i.state.userSettingsForm.matrix_user_id = event.target.value;
919 i.state.userSettingsForm.matrix_user_id == '' &&
920 !i.state.user.matrix_user_id
922 i.state.userSettingsForm.matrix_user_id = undefined;
927 handleUserSettingsNewPasswordChange(i: User, event: any) {
928 i.state.userSettingsForm.new_password = event.target.value;
929 if (i.state.userSettingsForm.new_password == '') {
930 i.state.userSettingsForm.new_password = undefined;
935 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
936 i.state.userSettingsForm.new_password_verify = event.target.value;
937 if (i.state.userSettingsForm.new_password_verify == '') {
938 i.state.userSettingsForm.new_password_verify = undefined;
943 handleUserSettingsOldPasswordChange(i: User, event: any) {
944 i.state.userSettingsForm.old_password = event.target.value;
945 if (i.state.userSettingsForm.old_password == '') {
946 i.state.userSettingsForm.old_password = undefined;
951 handleImageUpload(i: User, event: any) {
952 event.preventDefault();
953 let file = event.target.files[0];
954 const imageUploadUrl = `/pictshare/api/upload.php`;
955 const formData = new FormData();
956 formData.append('file', file);
958 i.state.avatarLoading = true;
961 fetch(imageUploadUrl, {
965 .then(res => res.json())
967 let url = `${window.location.origin}/pictshare/${res.url}`;
968 if (res.filetype == 'mp4') {
971 i.state.userSettingsForm.avatar = url;
973 i.state.avatarLoading = false;
977 i.state.avatarLoading = false;
979 toast(error, 'danger');
983 handleUserSettingsSubmit(i: User, event: any) {
984 event.preventDefault();
985 i.state.userSettingsLoading = true;
988 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
991 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
992 event.preventDefault();
993 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
997 handleDeleteAccountPasswordChange(i: User, event: any) {
998 i.state.deleteAccountForm.password = event.target.value;
1002 handleLogoutClick(i: User) {
1003 UserService.Instance.logout();
1004 i.context.router.history.push('/');
1007 handleDeleteAccount(i: User, event: any) {
1008 event.preventDefault();
1009 i.state.deleteAccountLoading = true;
1010 i.setState(i.state);
1012 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1015 parseMessage(msg: any) {
1017 let op: UserOperation = msgOp(msg);
1019 toast(i18n.t(msg.error), 'danger');
1020 this.state.deleteAccountLoading = false;
1021 this.state.avatarLoading = false;
1022 this.state.userSettingsLoading = false;
1023 if (msg.error == 'couldnt_find_that_username_or_email') {
1024 this.context.router.history.push('/');
1026 this.setState(this.state);
1028 } else if (op == UserOperation.GetUserDetails) {
1029 let res: UserDetailsResponse = msg;
1030 this.state.user = res.user;
1031 this.state.comments = res.comments;
1032 this.state.follows = res.follows;
1033 this.state.moderates = res.moderates;
1034 this.state.posts = res.posts;
1035 this.state.admins = res.admins;
1036 this.state.loading = false;
1037 if (this.isCurrentUser) {
1038 this.state.userSettingsForm.show_nsfw =
1039 UserService.Instance.user.show_nsfw;
1040 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1041 ? UserService.Instance.user.theme
1043 this.state.userSettingsForm.default_sort_type =
1044 UserService.Instance.user.default_sort_type;
1045 this.state.userSettingsForm.default_listing_type =
1046 UserService.Instance.user.default_listing_type;
1047 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1048 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1049 this.state.userSettingsForm.email = this.state.user.email;
1050 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1051 this.state.userSettingsForm.show_avatars =
1052 UserService.Instance.user.show_avatars;
1053 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1055 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1056 window.scrollTo(0, 0);
1057 this.setState(this.state);
1058 } else if (op == UserOperation.EditComment) {
1059 let res: CommentResponse = msg;
1061 let found = this.state.comments.find(c => c.id == res.comment.id);
1062 found.content = res.comment.content;
1063 found.updated = res.comment.updated;
1064 found.removed = res.comment.removed;
1065 found.deleted = res.comment.deleted;
1066 found.upvotes = res.comment.upvotes;
1067 found.downvotes = res.comment.downvotes;
1068 found.score = res.comment.score;
1070 this.setState(this.state);
1071 } else if (op == UserOperation.CreateComment) {
1072 // let res: CommentResponse = msg;
1073 toast(i18n.t('reply_sent'));
1074 // this.state.comments.unshift(res.comment); // TODO do this right
1075 // this.setState(this.state);
1076 } else if (op == UserOperation.SaveComment) {
1077 let res: CommentResponse = msg;
1078 let found = this.state.comments.find(c => c.id == res.comment.id);
1079 found.saved = res.comment.saved;
1080 this.setState(this.state);
1081 } else if (op == UserOperation.CreateCommentLike) {
1082 let res: CommentResponse = msg;
1083 let found: Comment = this.state.comments.find(
1084 c => c.id === res.comment.id
1086 found.score = res.comment.score;
1087 found.upvotes = res.comment.upvotes;
1088 found.downvotes = res.comment.downvotes;
1089 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
1090 this.setState(this.state);
1091 } else if (op == UserOperation.CreatePostLike) {
1092 let res: CreatePostLikeResponse = msg;
1093 let found = this.state.posts.find(c => c.id == res.post.id);
1094 found.my_vote = res.post.my_vote;
1095 found.score = res.post.score;
1096 found.upvotes = res.post.upvotes;
1097 found.downvotes = res.post.downvotes;
1098 this.setState(this.state);
1099 } else if (op == UserOperation.BanUser) {
1100 let res: BanUserResponse = msg;
1102 .filter(c => c.creator_id == res.user.id)
1103 .forEach(c => (c.banned = res.banned));
1105 .filter(c => c.creator_id == res.user.id)
1106 .forEach(c => (c.banned = res.banned));
1107 this.setState(this.state);
1108 } else if (op == UserOperation.AddAdmin) {
1109 let res: AddAdminResponse = msg;
1110 this.state.admins = res.admins;
1111 this.setState(this.state);
1112 } else if (op == UserOperation.SaveUserSettings) {
1113 this.state = this.emptyState;
1114 this.state.userSettingsLoading = false;
1115 this.setState(this.state);
1116 let res: LoginResponse = msg;
1117 UserService.Instance.login(res);
1118 } else if (op == UserOperation.DeleteAccount) {
1119 this.state.deleteAccountLoading = false;
1120 this.state.deleteAccountShowConfirm = false;
1121 this.setState(this.state);
1122 this.context.router.history.push('/');