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 } from '../interfaces';
22 import { WebSocketService, UserService } from '../services';
27 capitalizeFirstLetter,
33 import { PostListing } from './post-listing';
34 import { SortSelect } from './sort-select';
35 import { ListingTypeSelect } from './listing-type-select';
36 import { CommentNodes } from './comment-nodes';
37 import { MomentTime } from './moment-time';
38 import { i18n } from '../i18next';
39 import { T } from 'inferno-i18next';
52 follows: Array<CommunityUser>;
53 moderates: Array<CommunityUser>;
54 comments: Array<Comment>;
57 admins: Array<UserView>;
62 avatarLoading: boolean;
63 userSettingsForm: UserSettingsForm;
64 userSettingsLoading: boolean;
65 deleteAccountLoading: boolean;
66 deleteAccountShowConfirm: boolean;
67 deleteAccountForm: DeleteAccountForm;
70 export class User extends Component<any, UserState> {
71 private subscription: Subscription;
72 private emptyState: UserState = {
78 number_of_posts: null,
80 number_of_comments: null,
85 send_notifications_to_email: null,
96 view: this.getViewFromProps(this.props),
97 sort: this.getSortTypeFromProps(this.props),
98 page: this.getPageFromProps(this.props),
102 default_sort_type: null,
103 default_listing_type: null,
106 send_notifications_to_email: null,
109 userSettingsLoading: null,
110 deleteAccountLoading: null,
111 deleteAccountShowConfirm: false,
117 constructor(props: any, context: any) {
118 super(props, context);
120 this.state = this.emptyState;
121 this.handleSortChange = this.handleSortChange.bind(this);
122 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
125 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
129 this.state.user_id = Number(this.props.match.params.id);
130 this.state.username = this.props.match.params.username;
132 this.subscription = WebSocketService.Instance.subject
142 msg => this.parseMessage(msg),
143 err => console.error(err),
144 () => console.log('complete')
150 get isCurrentUser() {
152 UserService.Instance.user &&
153 UserService.Instance.user.id == this.state.user.id
157 getViewFromProps(props: any): View {
158 return props.match.params.view
159 ? View[capitalizeFirstLetter(props.match.params.view)]
163 getSortTypeFromProps(props: any): SortType {
164 return props.match.params.sort
165 ? routeSortTypeToEnum(props.match.params.sort)
169 getPageFromProps(props: any): number {
170 return props.match.params.page ? Number(props.match.params.page) : 1;
173 componentWillUnmount() {
174 this.subscription.unsubscribe();
177 // Necessary for back button for some reason
178 componentWillReceiveProps(nextProps: any) {
180 nextProps.history.action == 'POP' ||
181 nextProps.history.action == 'PUSH'
183 this.state.view = this.getViewFromProps(nextProps);
184 this.state.sort = this.getSortTypeFromProps(nextProps);
185 this.state.page = this.getPageFromProps(nextProps);
186 this.setState(this.state);
191 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
192 // Necessary if you are on a post and you click another post (same route)
194 lastProps.location.pathname.split('/')[2] !==
195 lastProps.history.location.pathname.split('/')[2]
197 // Couldnt get a refresh working. This does for now.
204 <div class="container">
205 {this.state.loading ? (
207 <svg class="icon icon-spinner spin">
208 <use xlinkHref="#icon-spinner"></use>
213 <div class="col-12 col-md-8">
215 {this.state.user.avatar && showAvatars() && (
219 src={this.state.user.avatar}
220 class="rounded-circle mr-2"
223 <span>/u/{this.state.user.name}</span>
226 {this.state.view == View.Overview && this.overview()}
227 {this.state.view == View.Comments && this.comments()}
228 {this.state.view == View.Posts && this.posts()}
229 {this.state.view == View.Saved && this.overview()}
232 <div class="col-12 col-md-4">
234 {this.isCurrentUser && this.userSettings()}
246 <div className="mb-2">
248 value={this.state.view}
249 onChange={linkEvent(this, this.handleViewChange)}
250 class="custom-select custom-select-sm w-auto"
253 <T i18nKey="view">#</T>
255 <option value={View.Overview}>
256 <T i18nKey="overview">#</T>
258 <option value={View.Comments}>
259 <T i18nKey="comments">#</T>
261 <option value={View.Posts}>
262 <T i18nKey="posts">#</T>
264 <option value={View.Saved}>
265 <T i18nKey="saved">#</T>
270 sort={this.state.sort}
271 onChange={this.handleSortChange}
276 href={`/feeds/u/${this.state.username}.xml?sort=${
277 SortType[this.state.sort]
281 <svg class="icon mx-2 text-muted small">
282 <use xlinkHref="#icon-rss">#</use>
290 let combined: Array<{ type_: string; data: Comment | Post }> = [];
291 let comments = this.state.comments.map(e => {
292 return { type_: 'comments', data: e };
294 let posts = this.state.posts.map(e => {
295 return { type_: 'posts', data: e };
298 combined.push(...comments);
299 combined.push(...posts);
302 if (this.state.sort == SortType.New) {
303 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
305 combined.sort((a, b) => b.data.score - a.data.score);
312 {i.type_ == 'posts' ? (
314 post={i.data as Post}
315 admins={this.state.admins}
321 nodes={[{ comment: i.data as Comment }]}
322 admins={this.state.admins}
335 {this.state.comments.map(comment => (
337 nodes={[{ comment: comment }]}
338 admins={this.state.admins}
349 {this.state.posts.map(post => (
352 admins={this.state.admins}
362 let user = this.state.user;
365 <div class="card border-secondary mb-3">
366 <div class="card-body">
368 <ul class="list-inline mb-0">
369 <li className="list-inline-item">{user.name}</li>
371 <li className="list-inline-item badge badge-danger">
372 <T i18nKey="banned">#</T>
378 {i18n.t('joined')} <MomentTime data={user} />
380 <div class="table-responsive">
381 <table class="table table-bordered table-sm mt-2 mb-0">
385 i18nKey="number_of_points"
386 interpolation={{ count: user.post_score }}
393 i18nKey="number_of_posts"
394 interpolation={{ count: user.number_of_posts }}
403 i18nKey="number_of_points"
404 interpolation={{ count: user.comment_score }}
411 i18nKey="number_of_comments"
412 interpolation={{ count: user.number_of_comments }}
420 {this.isCurrentUser && (
422 class="btn btn-block btn-secondary mt-3"
423 onClick={linkEvent(this, this.handleLogoutClick)}
425 <T i18nKey="logout">#</T>
437 <div class="card border-secondary mb-3">
438 <div class="card-body">
440 <T i18nKey="settings">#</T>
442 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
443 <div class="form-group">
445 <T i18nKey="avatar">#</T>
447 <form class="d-inline">
449 htmlFor="file-upload"
450 class="pointer ml-4 text-muted small font-weight-bold"
456 this.state.userSettingsForm.avatar
457 ? this.state.userSettingsForm.avatar
458 : 'https://via.placeholder.com/300/000?text=Avatar'
460 class="rounded-circle"
466 accept="image/*,video/*"
469 disabled={!UserService.Instance.user}
470 onChange={linkEvent(this, this.handleImageUpload)}
474 <div class="form-group">
476 <T i18nKey="language">#</T>
479 value={this.state.userSettingsForm.lang}
480 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
481 class="ml-2 custom-select custom-select-sm w-auto"
484 <T i18nKey="language">#</T>
486 <option value="browser">
487 <T i18nKey="browser_default">#</T>
489 <option disabled>──</option>
490 {languages.map(lang => (
491 <option value={lang.code}>{lang.name}</option>
495 <div class="form-group">
497 <T i18nKey="theme">#</T>
500 value={this.state.userSettingsForm.theme}
501 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
502 class="ml-2 custom-select custom-select-sm w-auto"
505 <T i18nKey="theme">#</T>
507 {themes.map(theme => (
508 <option value={theme}>{theme}</option>
512 <form className="form-group">
514 <T i18nKey="sort_type" class="mr-2">
519 type_={this.state.userSettingsForm.default_listing_type}
520 onChange={this.handleUserSettingsListingTypeChange}
523 <form className="form-group">
525 <T i18nKey="type" class="mr-2">
530 sort={this.state.userSettingsForm.default_sort_type}
531 onChange={this.handleUserSettingsSortTypeChange}
534 <div class="form-group row">
535 <label class="col-lg-3 col-form-label">
536 <T i18nKey="email">#</T>
538 <div class="col-lg-9">
542 placeholder={i18n.t('optional')}
543 value={this.state.userSettingsForm.email}
546 this.handleUserSettingsEmailChange
552 <div class="form-group row">
553 <label class="col-lg-5 col-form-label">
554 <T i18nKey="new_password">#</T>
556 <div class="col-lg-7">
560 value={this.state.userSettingsForm.new_password}
563 this.handleUserSettingsNewPasswordChange
568 <div class="form-group row">
569 <label class="col-lg-5 col-form-label">
570 <T i18nKey="verify_password">#</T>
572 <div class="col-lg-7">
576 value={this.state.userSettingsForm.new_password_verify}
579 this.handleUserSettingsNewPasswordVerifyChange
584 <div class="form-group row">
585 <label class="col-lg-5 col-form-label">
586 <T i18nKey="old_password">#</T>
588 <div class="col-lg-7">
592 value={this.state.userSettingsForm.old_password}
595 this.handleUserSettingsOldPasswordChange
600 {WebSocketService.Instance.site.enable_nsfw && (
601 <div class="form-group">
602 <div class="form-check">
604 class="form-check-input"
606 checked={this.state.userSettingsForm.show_nsfw}
609 this.handleUserSettingsShowNsfwChange
612 <label class="form-check-label">
613 <T i18nKey="show_nsfw">#</T>
618 <div class="form-group">
619 <div class="form-check">
621 class="form-check-input"
623 checked={this.state.userSettingsForm.show_avatars}
626 this.handleUserSettingsShowAvatarsChange
629 <label class="form-check-label">
630 <T i18nKey="show_avatars">#</T>
634 <div class="form-group">
635 <div class="form-check">
637 class="form-check-input"
639 disabled={!this.state.user.email}
641 this.state.userSettingsForm.send_notifications_to_email
645 this.handleUserSettingsSendNotificationsToEmailChange
648 <label class="form-check-label">
649 <T i18nKey="send_notifications_to_email">#</T>
653 <div class="form-group">
654 <button type="submit" class="btn btn-block btn-secondary mr-4">
655 {this.state.userSettingsLoading ? (
656 <svg class="icon icon-spinner spin">
657 <use xlinkHref="#icon-spinner"></use>
660 capitalizeFirstLetter(i18n.t('save'))
665 <div class="form-group mb-0">
667 class="btn btn-block btn-danger"
670 this.handleDeleteAccountShowConfirmToggle
673 <T i18nKey="delete_account">#</T>
675 {this.state.deleteAccountShowConfirm && (
677 <div class="my-2 alert alert-danger" role="alert">
678 <T i18nKey="delete_account_confirm">#</T>
682 value={this.state.deleteAccountForm.password}
685 this.handleDeleteAccountPasswordChange
687 class="form-control my-2"
690 class="btn btn-danger mr-4"
691 disabled={!this.state.deleteAccountForm.password}
692 onClick={linkEvent(this, this.handleDeleteAccount)}
694 {this.state.deleteAccountLoading ? (
695 <svg class="icon icon-spinner spin">
696 <use xlinkHref="#icon-spinner"></use>
699 capitalizeFirstLetter(i18n.t('delete'))
703 class="btn btn-secondary"
706 this.handleDeleteAccountShowConfirmToggle
709 <T i18nKey="cancel">#</T>
724 {this.state.moderates.length > 0 && (
725 <div class="card border-secondary mb-3">
726 <div class="card-body">
728 <T i18nKey="moderates">#</T>
730 <ul class="list-unstyled mb-0">
731 {this.state.moderates.map(community => (
733 <Link to={`/c/${community.community_name}`}>
734 {community.community_name}
749 {this.state.follows.length > 0 && (
750 <div class="card border-secondary mb-3">
751 <div class="card-body">
753 <T i18nKey="subscribed">#</T>
755 <ul class="list-unstyled mb-0">
756 {this.state.follows.map(community => (
758 <Link to={`/c/${community.community_name}`}>
759 {community.community_name}
774 {this.state.page > 1 && (
776 class="btn btn-sm btn-secondary mr-1"
777 onClick={linkEvent(this, this.prevPage)}
779 <T i18nKey="prev">#</T>
783 class="btn btn-sm btn-secondary"
784 onClick={linkEvent(this, this.nextPage)}
786 <T i18nKey="next">#</T>
793 let viewStr = View[this.state.view].toLowerCase();
794 let sortStr = SortType[this.state.sort].toLowerCase();
795 this.props.history.push(
796 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
815 let form: GetUserDetailsForm = {
816 user_id: this.state.user_id,
817 username: this.state.username,
818 sort: SortType[this.state.sort],
819 saved_only: this.state.view == View.Saved,
820 page: this.state.page,
823 WebSocketService.Instance.getUserDetails(form);
826 handleSortChange(val: SortType) {
827 this.state.sort = val;
829 this.setState(this.state);
834 handleViewChange(i: User, event: any) {
835 i.state.view = Number(event.target.value);
842 handleUserSettingsShowNsfwChange(i: User, event: any) {
843 i.state.userSettingsForm.show_nsfw = event.target.checked;
847 handleUserSettingsShowAvatarsChange(i: User, event: any) {
848 i.state.userSettingsForm.show_avatars = event.target.checked;
849 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
853 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
854 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
858 handleUserSettingsThemeChange(i: User, event: any) {
859 i.state.userSettingsForm.theme = event.target.value;
860 setTheme(event.target.value);
864 handleUserSettingsLangChange(i: User, event: any) {
865 i.state.userSettingsForm.lang = event.target.value;
866 i18n.changeLanguage(i.state.userSettingsForm.lang);
870 handleUserSettingsSortTypeChange(val: SortType) {
871 this.state.userSettingsForm.default_sort_type = val;
872 this.setState(this.state);
875 handleUserSettingsListingTypeChange(val: ListingType) {
876 this.state.userSettingsForm.default_listing_type = val;
877 this.setState(this.state);
880 handleUserSettingsEmailChange(i: User, event: any) {
881 i.state.userSettingsForm.email = event.target.value;
882 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
883 i.state.userSettingsForm.email = undefined;
888 handleUserSettingsNewPasswordChange(i: User, event: any) {
889 i.state.userSettingsForm.new_password = event.target.value;
890 if (i.state.userSettingsForm.new_password == '') {
891 i.state.userSettingsForm.new_password = undefined;
896 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
897 i.state.userSettingsForm.new_password_verify = event.target.value;
898 if (i.state.userSettingsForm.new_password_verify == '') {
899 i.state.userSettingsForm.new_password_verify = undefined;
904 handleUserSettingsOldPasswordChange(i: User, event: any) {
905 i.state.userSettingsForm.old_password = event.target.value;
906 if (i.state.userSettingsForm.old_password == '') {
907 i.state.userSettingsForm.old_password = undefined;
912 handleImageUpload(i: User, event: any) {
913 event.preventDefault();
914 let file = event.target.files[0];
915 const imageUploadUrl = `/pictshare/api/upload.php`;
916 const formData = new FormData();
917 formData.append('file', file);
919 i.state.avatarLoading = true;
922 fetch(imageUploadUrl, {
926 .then(res => res.json())
928 let url = `${window.location.origin}/pictshare/${res.url}`;
929 if (res.filetype == 'mp4') {
932 i.state.userSettingsForm.avatar = url;
934 i.state.avatarLoading = false;
938 i.state.avatarLoading = false;
944 handleUserSettingsSubmit(i: User, event: any) {
945 event.preventDefault();
946 i.state.userSettingsLoading = true;
949 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
952 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
953 event.preventDefault();
954 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
958 handleDeleteAccountPasswordChange(i: User, event: any) {
959 i.state.deleteAccountForm.password = event.target.value;
963 handleLogoutClick(i: User) {
964 UserService.Instance.logout();
965 i.context.router.history.push('/');
968 handleDeleteAccount(i: User, event: any) {
969 event.preventDefault();
970 i.state.deleteAccountLoading = true;
973 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
976 parseMessage(msg: any) {
978 let op: UserOperation = msgOp(msg);
980 alert(i18n.t(msg.error));
981 this.state.deleteAccountLoading = false;
982 this.state.avatarLoading = false;
983 this.state.userSettingsLoading = false;
984 if (msg.error == 'couldnt_find_that_username_or_email') {
985 this.context.router.history.push('/');
987 this.setState(this.state);
989 } else if (op == UserOperation.GetUserDetails) {
990 let res: UserDetailsResponse = msg;
991 this.state.user = res.user;
992 this.state.comments = res.comments;
993 this.state.follows = res.follows;
994 this.state.moderates = res.moderates;
995 this.state.posts = res.posts;
996 this.state.admins = res.admins;
997 this.state.loading = false;
998 if (this.isCurrentUser) {
999 this.state.userSettingsForm.show_nsfw =
1000 UserService.Instance.user.show_nsfw;
1001 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1002 ? UserService.Instance.user.theme
1004 this.state.userSettingsForm.default_sort_type =
1005 UserService.Instance.user.default_sort_type;
1006 this.state.userSettingsForm.default_listing_type =
1007 UserService.Instance.user.default_listing_type;
1008 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1009 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1010 this.state.userSettingsForm.email = this.state.user.email;
1011 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1012 this.state.userSettingsForm.show_avatars =
1013 UserService.Instance.user.show_avatars;
1015 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1016 window.scrollTo(0, 0);
1017 this.setState(this.state);
1018 } else if (op == UserOperation.EditComment) {
1019 let res: CommentResponse = msg;
1021 let found = this.state.comments.find(c => c.id == res.comment.id);
1022 found.content = res.comment.content;
1023 found.updated = res.comment.updated;
1024 found.removed = res.comment.removed;
1025 found.deleted = res.comment.deleted;
1026 found.upvotes = res.comment.upvotes;
1027 found.downvotes = res.comment.downvotes;
1028 found.score = res.comment.score;
1030 this.setState(this.state);
1031 } else if (op == UserOperation.CreateComment) {
1032 // let res: CommentResponse = msg;
1033 alert(i18n.t('reply_sent'));
1034 // this.state.comments.unshift(res.comment); // TODO do this right
1035 // this.setState(this.state);
1036 } else if (op == UserOperation.SaveComment) {
1037 let res: CommentResponse = msg;
1038 let found = this.state.comments.find(c => c.id == res.comment.id);
1039 found.saved = res.comment.saved;
1040 this.setState(this.state);
1041 } else if (op == UserOperation.CreateCommentLike) {
1042 let res: CommentResponse = msg;
1043 let found: Comment = this.state.comments.find(
1044 c => c.id === res.comment.id
1046 found.score = res.comment.score;
1047 found.upvotes = res.comment.upvotes;
1048 found.downvotes = res.comment.downvotes;
1049 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
1050 this.setState(this.state);
1051 } else if (op == UserOperation.BanUser) {
1052 let res: BanUserResponse = msg;
1054 .filter(c => c.creator_id == res.user.id)
1055 .forEach(c => (c.banned = res.banned));
1057 .filter(c => c.creator_id == res.user.id)
1058 .forEach(c => (c.banned = res.banned));
1059 this.setState(this.state);
1060 } else if (op == UserOperation.AddAdmin) {
1061 let res: AddAdminResponse = msg;
1062 this.state.admins = res.admins;
1063 this.setState(this.state);
1064 } else if (op == UserOperation.SaveUserSettings) {
1065 this.state = this.emptyState;
1066 this.state.userSettingsLoading = false;
1067 this.setState(this.state);
1068 let res: LoginResponse = msg;
1069 UserService.Instance.login(res);
1070 } else if (op == UserOperation.DeleteAccount) {
1071 this.state.deleteAccountLoading = false;
1072 this.state.deleteAccountShowConfirm = false;
1073 this.setState(this.state);
1074 this.context.router.history.push('/');