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 ? (
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"
249 <T i18nKey="view">#</T>
251 <option value={View.Overview}>
252 <T i18nKey="overview">#</T>
254 <option value={View.Comments}>
255 <T i18nKey="comments">#</T>
257 <option value={View.Posts}>
258 <T i18nKey="posts">#</T>
260 <option value={View.Saved}>
261 <T i18nKey="saved">#</T>
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">
362 <T i18nKey="banned">#</T>
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)}
415 <T i18nKey="logout">#</T>
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 <T i18nKey="settings">#</T>
449 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
450 <div class="form-group">
452 <T i18nKey="avatar">#</T>
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 <T i18nKey="upload_avatar">#</T>
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 <T i18nKey="language">#</T>
488 value={this.state.userSettingsForm.lang}
489 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
490 class="ml-2 custom-select custom-select-sm w-auto"
493 <T i18nKey="language">#</T>
495 <option value="browser">
496 <T i18nKey="browser_default">#</T>
498 <option disabled>──</option>
499 {languages.map(lang => (
500 <option value={lang.code}>{lang.name}</option>
504 <div class="form-group">
506 <T i18nKey="theme">#</T>
509 value={this.state.userSettingsForm.theme}
510 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
511 class="ml-2 custom-select custom-select-sm w-auto"
514 <T i18nKey="theme">#</T>
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">
545 <T i18nKey="email">#</T>
547 <div class="col-lg-9">
551 placeholder={i18n.t('optional')}
552 value={this.state.userSettingsForm.email}
555 this.handleUserSettingsEmailChange
561 <div class="form-group row">
562 <label class="col-lg-5 col-form-label">
563 <a href="https://about.riot.im/" target="_blank">
564 {i18n.t('matrix_user_id')}
567 <div class="col-lg-7">
571 placeholder="@user:example.com"
572 value={this.state.userSettingsForm.matrix_user_id}
575 this.handleUserSettingsMatrixUserIdChange
581 <div class="form-group row">
582 <label class="col-lg-5 col-form-label">
583 <T i18nKey="new_password">#</T>
585 <div class="col-lg-7">
589 value={this.state.userSettingsForm.new_password}
592 this.handleUserSettingsNewPasswordChange
597 <div class="form-group row">
598 <label class="col-lg-5 col-form-label">
599 <T i18nKey="verify_password">#</T>
601 <div class="col-lg-7">
605 value={this.state.userSettingsForm.new_password_verify}
608 this.handleUserSettingsNewPasswordVerifyChange
613 <div class="form-group row">
614 <label class="col-lg-5 col-form-label">
615 <T i18nKey="old_password">#</T>
617 <div class="col-lg-7">
621 value={this.state.userSettingsForm.old_password}
624 this.handleUserSettingsOldPasswordChange
629 {WebSocketService.Instance.site.enable_nsfw && (
630 <div class="form-group">
631 <div class="form-check">
633 class="form-check-input"
635 checked={this.state.userSettingsForm.show_nsfw}
638 this.handleUserSettingsShowNsfwChange
641 <label class="form-check-label">
642 <T i18nKey="show_nsfw">#</T>
647 <div class="form-group">
648 <div class="form-check">
650 class="form-check-input"
652 checked={this.state.userSettingsForm.show_avatars}
655 this.handleUserSettingsShowAvatarsChange
658 <label class="form-check-label">
659 <T i18nKey="show_avatars">#</T>
663 <div class="form-group">
664 <div class="form-check">
666 class="form-check-input"
668 disabled={!this.state.user.email}
670 this.state.userSettingsForm.send_notifications_to_email
674 this.handleUserSettingsSendNotificationsToEmailChange
677 <label class="form-check-label">
678 <T i18nKey="send_notifications_to_email">#</T>
682 <div class="form-group">
683 <button type="submit" class="btn btn-block btn-secondary mr-4">
684 {this.state.userSettingsLoading ? (
685 <svg class="icon icon-spinner spin">
686 <use xlinkHref="#icon-spinner"></use>
689 capitalizeFirstLetter(i18n.t('save'))
694 <div class="form-group mb-0">
696 class="btn btn-block btn-danger"
699 this.handleDeleteAccountShowConfirmToggle
702 <T i18nKey="delete_account">#</T>
704 {this.state.deleteAccountShowConfirm && (
706 <div class="my-2 alert alert-danger" role="alert">
707 <T i18nKey="delete_account_confirm">#</T>
711 value={this.state.deleteAccountForm.password}
714 this.handleDeleteAccountPasswordChange
716 class="form-control my-2"
719 class="btn btn-danger mr-4"
720 disabled={!this.state.deleteAccountForm.password}
721 onClick={linkEvent(this, this.handleDeleteAccount)}
723 {this.state.deleteAccountLoading ? (
724 <svg class="icon icon-spinner spin">
725 <use xlinkHref="#icon-spinner"></use>
728 capitalizeFirstLetter(i18n.t('delete'))
732 class="btn btn-secondary"
735 this.handleDeleteAccountShowConfirmToggle
738 <T i18nKey="cancel">#</T>
753 {this.state.moderates.length > 0 && (
754 <div class="card border-secondary mb-3">
755 <div class="card-body">
757 <T i18nKey="moderates">#</T>
759 <ul class="list-unstyled mb-0">
760 {this.state.moderates.map(community => (
762 <Link to={`/c/${community.community_name}`}>
763 {community.community_name}
778 {this.state.follows.length > 0 && (
779 <div class="card border-secondary mb-3">
780 <div class="card-body">
782 <T i18nKey="subscribed">#</T>
784 <ul class="list-unstyled mb-0">
785 {this.state.follows.map(community => (
787 <Link to={`/c/${community.community_name}`}>
788 {community.community_name}
803 {this.state.page > 1 && (
805 class="btn btn-sm btn-secondary mr-1"
806 onClick={linkEvent(this, this.prevPage)}
808 <T i18nKey="prev">#</T>
812 class="btn btn-sm btn-secondary"
813 onClick={linkEvent(this, this.nextPage)}
815 <T i18nKey="next">#</T>
822 let viewStr = View[this.state.view].toLowerCase();
823 let sortStr = SortType[this.state.sort].toLowerCase();
824 this.props.history.push(
825 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
844 let form: GetUserDetailsForm = {
845 user_id: this.state.user_id,
846 username: this.state.username,
847 sort: SortType[this.state.sort],
848 saved_only: this.state.view == View.Saved,
849 page: this.state.page,
852 WebSocketService.Instance.getUserDetails(form);
855 handleSortChange(val: SortType) {
856 this.state.sort = val;
858 this.setState(this.state);
863 handleViewChange(i: User, event: any) {
864 i.state.view = Number(event.target.value);
871 handleUserSettingsShowNsfwChange(i: User, event: any) {
872 i.state.userSettingsForm.show_nsfw = event.target.checked;
876 handleUserSettingsShowAvatarsChange(i: User, event: any) {
877 i.state.userSettingsForm.show_avatars = event.target.checked;
878 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
882 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
883 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
887 handleUserSettingsThemeChange(i: User, event: any) {
888 i.state.userSettingsForm.theme = event.target.value;
889 setTheme(event.target.value);
893 handleUserSettingsLangChange(i: User, event: any) {
894 i.state.userSettingsForm.lang = event.target.value;
895 i18n.changeLanguage(i.state.userSettingsForm.lang);
899 handleUserSettingsSortTypeChange(val: SortType) {
900 this.state.userSettingsForm.default_sort_type = val;
901 this.setState(this.state);
904 handleUserSettingsListingTypeChange(val: ListingType) {
905 this.state.userSettingsForm.default_listing_type = val;
906 this.setState(this.state);
909 handleUserSettingsEmailChange(i: User, event: any) {
910 i.state.userSettingsForm.email = event.target.value;
911 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
912 i.state.userSettingsForm.email = undefined;
917 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
918 i.state.userSettingsForm.matrix_user_id = event.target.value;
920 i.state.userSettingsForm.matrix_user_id == '' &&
921 !i.state.user.matrix_user_id
923 i.state.userSettingsForm.matrix_user_id = undefined;
928 handleUserSettingsNewPasswordChange(i: User, event: any) {
929 i.state.userSettingsForm.new_password = event.target.value;
930 if (i.state.userSettingsForm.new_password == '') {
931 i.state.userSettingsForm.new_password = undefined;
936 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
937 i.state.userSettingsForm.new_password_verify = event.target.value;
938 if (i.state.userSettingsForm.new_password_verify == '') {
939 i.state.userSettingsForm.new_password_verify = undefined;
944 handleUserSettingsOldPasswordChange(i: User, event: any) {
945 i.state.userSettingsForm.old_password = event.target.value;
946 if (i.state.userSettingsForm.old_password == '') {
947 i.state.userSettingsForm.old_password = undefined;
952 handleImageUpload(i: User, event: any) {
953 event.preventDefault();
954 let file = event.target.files[0];
955 const imageUploadUrl = `/pictshare/api/upload.php`;
956 const formData = new FormData();
957 formData.append('file', file);
959 i.state.avatarLoading = true;
962 fetch(imageUploadUrl, {
966 .then(res => res.json())
968 let url = `${window.location.origin}/pictshare/${res.url}`;
969 if (res.filetype == 'mp4') {
972 i.state.userSettingsForm.avatar = url;
974 i.state.avatarLoading = false;
978 i.state.avatarLoading = false;
980 toast(error, 'danger');
984 handleUserSettingsSubmit(i: User, event: any) {
985 event.preventDefault();
986 i.state.userSettingsLoading = true;
989 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
992 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
993 event.preventDefault();
994 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
998 handleDeleteAccountPasswordChange(i: User, event: any) {
999 i.state.deleteAccountForm.password = event.target.value;
1000 i.setState(i.state);
1003 handleLogoutClick(i: User) {
1004 UserService.Instance.logout();
1005 i.context.router.history.push('/');
1008 handleDeleteAccount(i: User, event: any) {
1009 event.preventDefault();
1010 i.state.deleteAccountLoading = true;
1011 i.setState(i.state);
1013 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1016 parseMessage(msg: WebSocketJsonResponse) {
1018 let res = wsJsonToRes(msg);
1020 toast(i18n.t(msg.error), 'danger');
1021 this.state.deleteAccountLoading = false;
1022 this.state.avatarLoading = false;
1023 this.state.userSettingsLoading = false;
1024 if (res.error == 'couldnt_find_that_username_or_email') {
1025 this.context.router.history.push('/');
1027 this.setState(this.state);
1029 } else if (res.op == UserOperation.GetUserDetails) {
1030 let data = res.data as UserDetailsResponse;
1031 this.state.user = data.user;
1032 this.state.comments = data.comments;
1033 this.state.follows = data.follows;
1034 this.state.moderates = data.moderates;
1035 this.state.posts = data.posts;
1036 this.state.admins = data.admins;
1037 this.state.loading = false;
1038 if (this.isCurrentUser) {
1039 this.state.userSettingsForm.show_nsfw =
1040 UserService.Instance.user.show_nsfw;
1041 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1042 ? UserService.Instance.user.theme
1044 this.state.userSettingsForm.default_sort_type =
1045 UserService.Instance.user.default_sort_type;
1046 this.state.userSettingsForm.default_listing_type =
1047 UserService.Instance.user.default_listing_type;
1048 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1049 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1050 this.state.userSettingsForm.email = this.state.user.email;
1051 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1052 this.state.userSettingsForm.show_avatars =
1053 UserService.Instance.user.show_avatars;
1054 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1056 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1057 window.scrollTo(0, 0);
1058 this.setState(this.state);
1059 } else if (res.op == UserOperation.EditComment) {
1060 let data = res.data as CommentResponse;
1062 let found = this.state.comments.find(c => c.id == data.comment.id);
1063 found.content = data.comment.content;
1064 found.updated = data.comment.updated;
1065 found.removed = data.comment.removed;
1066 found.deleted = data.comment.deleted;
1067 found.upvotes = data.comment.upvotes;
1068 found.downvotes = data.comment.downvotes;
1069 found.score = data.comment.score;
1071 this.setState(this.state);
1072 } else if (res.op == UserOperation.CreateComment) {
1073 // let res: CommentResponse = msg;
1074 toast(i18n.t('reply_sent'));
1075 // this.state.comments.unshift(res.comment); // TODO do this right
1076 // this.setState(this.state);
1077 } else if (res.op == UserOperation.SaveComment) {
1078 let data = res.data as CommentResponse;
1079 let found = this.state.comments.find(c => c.id == data.comment.id);
1080 found.saved = data.comment.saved;
1081 this.setState(this.state);
1082 } else if (res.op == UserOperation.CreateCommentLike) {
1083 let data = res.data as CommentResponse;
1084 let found: Comment = this.state.comments.find(
1085 c => c.id === data.comment.id
1087 found.score = data.comment.score;
1088 found.upvotes = data.comment.upvotes;
1089 found.downvotes = data.comment.downvotes;
1090 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1091 this.setState(this.state);
1092 } else if (res.op == UserOperation.CreatePostLike) {
1093 let data = res.data as CreatePostLikeResponse;
1094 let found = this.state.posts.find(c => c.id == data.post.id);
1095 found.my_vote = data.post.my_vote;
1096 found.score = data.post.score;
1097 found.upvotes = data.post.upvotes;
1098 found.downvotes = data.post.downvotes;
1099 this.setState(this.state);
1100 } else if (res.op == UserOperation.BanUser) {
1101 let data = res.data as BanUserResponse;
1103 .filter(c => c.creator_id == data.user.id)
1104 .forEach(c => (c.banned = data.banned));
1106 .filter(c => c.creator_id == data.user.id)
1107 .forEach(c => (c.banned = data.banned));
1108 this.setState(this.state);
1109 } else if (res.op == UserOperation.AddAdmin) {
1110 let data = res.data as AddAdminResponse;
1111 this.state.admins = data.admins;
1112 this.setState(this.state);
1113 } else if (res.op == UserOperation.SaveUserSettings) {
1114 let data = res.data as LoginResponse;
1115 this.state = this.emptyState;
1116 this.state.userSettingsLoading = false;
1117 this.setState(this.state);
1118 UserService.Instance.login(data);
1119 } else if (res.op == UserOperation.DeleteAccount) {
1120 this.state.deleteAccountLoading = false;
1121 this.state.deleteAccountShowConfirm = false;
1122 this.setState(this.state);
1123 this.context.router.history.push('/');