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
133 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
135 msg => this.parseMessage(msg),
136 err => console.error(err),
137 () => console.log('complete')
143 get isCurrentUser() {
145 UserService.Instance.user &&
146 UserService.Instance.user.id == this.state.user.id
150 getViewFromProps(props: any): View {
151 return props.match.params.view
152 ? View[capitalizeFirstLetter(props.match.params.view)]
156 getSortTypeFromProps(props: any): SortType {
157 return props.match.params.sort
158 ? routeSortTypeToEnum(props.match.params.sort)
162 getPageFromProps(props: any): number {
163 return props.match.params.page ? Number(props.match.params.page) : 1;
166 componentWillUnmount() {
167 this.subscription.unsubscribe();
170 // Necessary for back button for some reason
171 componentWillReceiveProps(nextProps: any) {
173 nextProps.history.action == 'POP' ||
174 nextProps.history.action == 'PUSH'
176 this.state.view = this.getViewFromProps(nextProps);
177 this.state.sort = this.getSortTypeFromProps(nextProps);
178 this.state.page = this.getPageFromProps(nextProps);
179 this.setState(this.state);
184 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
185 // Necessary if you are on a post and you click another post (same route)
187 lastProps.location.pathname.split('/')[2] !==
188 lastProps.history.location.pathname.split('/')[2]
190 // Couldnt get a refresh working. This does for now.
197 <div class="container">
198 {this.state.loading ? (
200 <svg class="icon icon-spinner spin">
201 <use xlinkHref="#icon-spinner"></use>
206 <div class="col-12 col-md-8">
208 {this.state.user.avatar && showAvatars() && (
212 src={this.state.user.avatar}
213 class="rounded-circle mr-2"
216 <span>/u/{this.state.user.name}</span>
219 {this.state.view == View.Overview && this.overview()}
220 {this.state.view == View.Comments && this.comments()}
221 {this.state.view == View.Posts && this.posts()}
222 {this.state.view == View.Saved && this.overview()}
225 <div class="col-12 col-md-4">
227 {this.isCurrentUser && this.userSettings()}
239 <div className="mb-2">
241 value={this.state.view}
242 onChange={linkEvent(this, this.handleViewChange)}
243 class="custom-select custom-select-sm w-auto"
246 <T i18nKey="view">#</T>
248 <option value={View.Overview}>
249 <T i18nKey="overview">#</T>
251 <option value={View.Comments}>
252 <T i18nKey="comments">#</T>
254 <option value={View.Posts}>
255 <T i18nKey="posts">#</T>
257 <option value={View.Saved}>
258 <T i18nKey="saved">#</T>
263 sort={this.state.sort}
264 onChange={this.handleSortChange}
269 href={`/feeds/u/${this.state.username}.xml?sort=${
270 SortType[this.state.sort]
274 <svg class="icon mx-2 text-muted small">
275 <use xlinkHref="#icon-rss">#</use>
283 let combined: Array<{ type_: string; data: Comment | Post }> = [];
284 let comments = this.state.comments.map(e => {
285 return { type_: 'comments', data: e };
287 let posts = this.state.posts.map(e => {
288 return { type_: 'posts', data: e };
291 combined.push(...comments);
292 combined.push(...posts);
295 if (this.state.sort == SortType.New) {
296 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
298 combined.sort((a, b) => b.data.score - a.data.score);
305 {i.type_ == 'posts' ? (
307 post={i.data as Post}
308 admins={this.state.admins}
314 nodes={[{ comment: i.data as Comment }]}
315 admins={this.state.admins}
328 {this.state.comments.map(comment => (
330 nodes={[{ comment: comment }]}
331 admins={this.state.admins}
342 {this.state.posts.map(post => (
345 admins={this.state.admins}
355 let user = this.state.user;
358 <div class="card border-secondary mb-3">
359 <div class="card-body">
361 <ul class="list-inline mb-0">
362 <li className="list-inline-item">{user.name}</li>
364 <li className="list-inline-item badge badge-danger">
365 <T i18nKey="banned">#</T>
371 {i18n.t('joined')} <MomentTime data={user} />
373 <div class="table-responsive">
374 <table class="table table-bordered table-sm mt-2 mb-0">
378 i18nKey="number_of_points"
379 interpolation={{ count: user.post_score }}
386 i18nKey="number_of_posts"
387 interpolation={{ count: user.number_of_posts }}
396 i18nKey="number_of_points"
397 interpolation={{ count: user.comment_score }}
404 i18nKey="number_of_comments"
405 interpolation={{ count: user.number_of_comments }}
413 {this.isCurrentUser && (
415 class="btn btn-block btn-secondary mt-3"
416 onClick={linkEvent(this, this.handleLogoutClick)}
418 <T i18nKey="logout">#</T>
430 <div class="card border-secondary mb-3">
431 <div class="card-body">
433 <T i18nKey="settings">#</T>
435 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
436 <div class="form-group">
438 <T i18nKey="avatar">#</T>
440 <form class="d-inline">
442 htmlFor="file-upload"
443 class="pointer ml-4 text-muted small font-weight-bold"
445 {!this.state.userSettingsForm.avatar ? (
446 <span class="btn btn-sm btn-secondary">
447 <T i18nKey="upload_avatar">#</T>
453 src={this.state.userSettingsForm.avatar}
454 class="rounded-circle"
461 accept="image/*,video/*"
464 disabled={!UserService.Instance.user}
465 onChange={linkEvent(this, this.handleImageUpload)}
469 <div class="form-group">
471 <T i18nKey="language">#</T>
474 value={this.state.userSettingsForm.lang}
475 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
476 class="ml-2 custom-select custom-select-sm w-auto"
479 <T i18nKey="language">#</T>
481 <option value="browser">
482 <T i18nKey="browser_default">#</T>
484 <option disabled>──</option>
485 {languages.map(lang => (
486 <option value={lang.code}>{lang.name}</option>
490 <div class="form-group">
492 <T i18nKey="theme">#</T>
495 value={this.state.userSettingsForm.theme}
496 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
497 class="ml-2 custom-select custom-select-sm w-auto"
500 <T i18nKey="theme">#</T>
502 {themes.map(theme => (
503 <option value={theme}>{theme}</option>
507 <form className="form-group">
509 <T i18nKey="sort_type" class="mr-2">
514 type_={this.state.userSettingsForm.default_listing_type}
515 onChange={this.handleUserSettingsListingTypeChange}
518 <form className="form-group">
520 <T i18nKey="type" class="mr-2">
525 sort={this.state.userSettingsForm.default_sort_type}
526 onChange={this.handleUserSettingsSortTypeChange}
529 <div class="form-group row">
530 <label class="col-lg-3 col-form-label">
531 <T i18nKey="email">#</T>
533 <div class="col-lg-9">
537 placeholder={i18n.t('optional')}
538 value={this.state.userSettingsForm.email}
541 this.handleUserSettingsEmailChange
547 <div class="form-group row">
548 <label class="col-lg-5 col-form-label">
549 <T i18nKey="new_password">#</T>
551 <div class="col-lg-7">
555 value={this.state.userSettingsForm.new_password}
558 this.handleUserSettingsNewPasswordChange
563 <div class="form-group row">
564 <label class="col-lg-5 col-form-label">
565 <T i18nKey="verify_password">#</T>
567 <div class="col-lg-7">
571 value={this.state.userSettingsForm.new_password_verify}
574 this.handleUserSettingsNewPasswordVerifyChange
579 <div class="form-group row">
580 <label class="col-lg-5 col-form-label">
581 <T i18nKey="old_password">#</T>
583 <div class="col-lg-7">
587 value={this.state.userSettingsForm.old_password}
590 this.handleUserSettingsOldPasswordChange
595 {WebSocketService.Instance.site.enable_nsfw && (
596 <div class="form-group">
597 <div class="form-check">
599 class="form-check-input"
601 checked={this.state.userSettingsForm.show_nsfw}
604 this.handleUserSettingsShowNsfwChange
607 <label class="form-check-label">
608 <T i18nKey="show_nsfw">#</T>
613 <div class="form-group">
614 <div class="form-check">
616 class="form-check-input"
618 checked={this.state.userSettingsForm.show_avatars}
621 this.handleUserSettingsShowAvatarsChange
624 <label class="form-check-label">
625 <T i18nKey="show_avatars">#</T>
629 <div class="form-group">
630 <div class="form-check">
632 class="form-check-input"
634 disabled={!this.state.user.email}
636 this.state.userSettingsForm.send_notifications_to_email
640 this.handleUserSettingsSendNotificationsToEmailChange
643 <label class="form-check-label">
644 <T i18nKey="send_notifications_to_email">#</T>
648 <div class="form-group">
649 <button type="submit" class="btn btn-block btn-secondary mr-4">
650 {this.state.userSettingsLoading ? (
651 <svg class="icon icon-spinner spin">
652 <use xlinkHref="#icon-spinner"></use>
655 capitalizeFirstLetter(i18n.t('save'))
660 <div class="form-group mb-0">
662 class="btn btn-block btn-danger"
665 this.handleDeleteAccountShowConfirmToggle
668 <T i18nKey="delete_account">#</T>
670 {this.state.deleteAccountShowConfirm && (
672 <div class="my-2 alert alert-danger" role="alert">
673 <T i18nKey="delete_account_confirm">#</T>
677 value={this.state.deleteAccountForm.password}
680 this.handleDeleteAccountPasswordChange
682 class="form-control my-2"
685 class="btn btn-danger mr-4"
686 disabled={!this.state.deleteAccountForm.password}
687 onClick={linkEvent(this, this.handleDeleteAccount)}
689 {this.state.deleteAccountLoading ? (
690 <svg class="icon icon-spinner spin">
691 <use xlinkHref="#icon-spinner"></use>
694 capitalizeFirstLetter(i18n.t('delete'))
698 class="btn btn-secondary"
701 this.handleDeleteAccountShowConfirmToggle
704 <T i18nKey="cancel">#</T>
719 {this.state.moderates.length > 0 && (
720 <div class="card border-secondary mb-3">
721 <div class="card-body">
723 <T i18nKey="moderates">#</T>
725 <ul class="list-unstyled mb-0">
726 {this.state.moderates.map(community => (
728 <Link to={`/c/${community.community_name}`}>
729 {community.community_name}
744 {this.state.follows.length > 0 && (
745 <div class="card border-secondary mb-3">
746 <div class="card-body">
748 <T i18nKey="subscribed">#</T>
750 <ul class="list-unstyled mb-0">
751 {this.state.follows.map(community => (
753 <Link to={`/c/${community.community_name}`}>
754 {community.community_name}
769 {this.state.page > 1 && (
771 class="btn btn-sm btn-secondary mr-1"
772 onClick={linkEvent(this, this.prevPage)}
774 <T i18nKey="prev">#</T>
778 class="btn btn-sm btn-secondary"
779 onClick={linkEvent(this, this.nextPage)}
781 <T i18nKey="next">#</T>
788 let viewStr = View[this.state.view].toLowerCase();
789 let sortStr = SortType[this.state.sort].toLowerCase();
790 this.props.history.push(
791 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
810 let form: GetUserDetailsForm = {
811 user_id: this.state.user_id,
812 username: this.state.username,
813 sort: SortType[this.state.sort],
814 saved_only: this.state.view == View.Saved,
815 page: this.state.page,
818 WebSocketService.Instance.getUserDetails(form);
821 handleSortChange(val: SortType) {
822 this.state.sort = val;
824 this.setState(this.state);
829 handleViewChange(i: User, event: any) {
830 i.state.view = Number(event.target.value);
837 handleUserSettingsShowNsfwChange(i: User, event: any) {
838 i.state.userSettingsForm.show_nsfw = event.target.checked;
842 handleUserSettingsShowAvatarsChange(i: User, event: any) {
843 i.state.userSettingsForm.show_avatars = event.target.checked;
844 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
848 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
849 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
853 handleUserSettingsThemeChange(i: User, event: any) {
854 i.state.userSettingsForm.theme = event.target.value;
855 setTheme(event.target.value);
859 handleUserSettingsLangChange(i: User, event: any) {
860 i.state.userSettingsForm.lang = event.target.value;
861 i18n.changeLanguage(i.state.userSettingsForm.lang);
865 handleUserSettingsSortTypeChange(val: SortType) {
866 this.state.userSettingsForm.default_sort_type = val;
867 this.setState(this.state);
870 handleUserSettingsListingTypeChange(val: ListingType) {
871 this.state.userSettingsForm.default_listing_type = val;
872 this.setState(this.state);
875 handleUserSettingsEmailChange(i: User, event: any) {
876 i.state.userSettingsForm.email = event.target.value;
877 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
878 i.state.userSettingsForm.email = undefined;
883 handleUserSettingsNewPasswordChange(i: User, event: any) {
884 i.state.userSettingsForm.new_password = event.target.value;
885 if (i.state.userSettingsForm.new_password == '') {
886 i.state.userSettingsForm.new_password = undefined;
891 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
892 i.state.userSettingsForm.new_password_verify = event.target.value;
893 if (i.state.userSettingsForm.new_password_verify == '') {
894 i.state.userSettingsForm.new_password_verify = undefined;
899 handleUserSettingsOldPasswordChange(i: User, event: any) {
900 i.state.userSettingsForm.old_password = event.target.value;
901 if (i.state.userSettingsForm.old_password == '') {
902 i.state.userSettingsForm.old_password = undefined;
907 handleImageUpload(i: User, event: any) {
908 event.preventDefault();
909 let file = event.target.files[0];
910 const imageUploadUrl = `/pictshare/api/upload.php`;
911 const formData = new FormData();
912 formData.append('file', file);
914 i.state.avatarLoading = true;
917 fetch(imageUploadUrl, {
921 .then(res => res.json())
923 let url = `${window.location.origin}/pictshare/${res.url}`;
924 if (res.filetype == 'mp4') {
927 i.state.userSettingsForm.avatar = url;
929 i.state.avatarLoading = false;
933 i.state.avatarLoading = false;
939 handleUserSettingsSubmit(i: User, event: any) {
940 event.preventDefault();
941 i.state.userSettingsLoading = true;
944 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
947 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
948 event.preventDefault();
949 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
953 handleDeleteAccountPasswordChange(i: User, event: any) {
954 i.state.deleteAccountForm.password = event.target.value;
958 handleLogoutClick(i: User) {
959 UserService.Instance.logout();
960 i.context.router.history.push('/');
963 handleDeleteAccount(i: User, event: any) {
964 event.preventDefault();
965 i.state.deleteAccountLoading = true;
968 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
971 parseMessage(msg: any) {
973 let op: UserOperation = msgOp(msg);
975 alert(i18n.t(msg.error));
976 this.state.deleteAccountLoading = false;
977 this.state.avatarLoading = false;
978 this.state.userSettingsLoading = false;
979 if (msg.error == 'couldnt_find_that_username_or_email') {
980 this.context.router.history.push('/');
982 this.setState(this.state);
984 } else if (op == UserOperation.GetUserDetails) {
985 let res: UserDetailsResponse = msg;
986 this.state.user = res.user;
987 this.state.comments = res.comments;
988 this.state.follows = res.follows;
989 this.state.moderates = res.moderates;
990 this.state.posts = res.posts;
991 this.state.admins = res.admins;
992 this.state.loading = false;
993 if (this.isCurrentUser) {
994 this.state.userSettingsForm.show_nsfw =
995 UserService.Instance.user.show_nsfw;
996 this.state.userSettingsForm.theme = UserService.Instance.user.theme
997 ? UserService.Instance.user.theme
999 this.state.userSettingsForm.default_sort_type =
1000 UserService.Instance.user.default_sort_type;
1001 this.state.userSettingsForm.default_listing_type =
1002 UserService.Instance.user.default_listing_type;
1003 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1004 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1005 this.state.userSettingsForm.email = this.state.user.email;
1006 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1007 this.state.userSettingsForm.show_avatars =
1008 UserService.Instance.user.show_avatars;
1010 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1011 window.scrollTo(0, 0);
1012 this.setState(this.state);
1013 } else if (op == UserOperation.EditComment) {
1014 let res: CommentResponse = msg;
1016 let found = this.state.comments.find(c => c.id == res.comment.id);
1017 found.content = res.comment.content;
1018 found.updated = res.comment.updated;
1019 found.removed = res.comment.removed;
1020 found.deleted = res.comment.deleted;
1021 found.upvotes = res.comment.upvotes;
1022 found.downvotes = res.comment.downvotes;
1023 found.score = res.comment.score;
1025 this.setState(this.state);
1026 } else if (op == UserOperation.CreateComment) {
1027 // let res: CommentResponse = msg;
1028 alert(i18n.t('reply_sent'));
1029 // this.state.comments.unshift(res.comment); // TODO do this right
1030 // this.setState(this.state);
1031 } else if (op == UserOperation.SaveComment) {
1032 let res: CommentResponse = msg;
1033 let found = this.state.comments.find(c => c.id == res.comment.id);
1034 found.saved = res.comment.saved;
1035 this.setState(this.state);
1036 } else if (op == UserOperation.CreateCommentLike) {
1037 let res: CommentResponse = msg;
1038 let found: Comment = this.state.comments.find(
1039 c => c.id === res.comment.id
1041 found.score = res.comment.score;
1042 found.upvotes = res.comment.upvotes;
1043 found.downvotes = res.comment.downvotes;
1044 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
1045 this.setState(this.state);
1046 } else if (op == UserOperation.BanUser) {
1047 let res: BanUserResponse = msg;
1049 .filter(c => c.creator_id == res.user.id)
1050 .forEach(c => (c.banned = res.banned));
1052 .filter(c => c.creator_id == res.user.id)
1053 .forEach(c => (c.banned = res.banned));
1054 this.setState(this.state);
1055 } else if (op == UserOperation.AddAdmin) {
1056 let res: AddAdminResponse = msg;
1057 this.state.admins = res.admins;
1058 this.setState(this.state);
1059 } else if (op == UserOperation.SaveUserSettings) {
1060 this.state = this.emptyState;
1061 this.state.userSettingsLoading = false;
1062 this.setState(this.state);
1063 let res: LoginResponse = msg;
1064 UserService.Instance.login(res);
1065 } else if (op == UserOperation.DeleteAccount) {
1066 this.state.deleteAccountLoading = false;
1067 this.state.deleteAccountShowConfirm = false;
1068 this.setState(this.state);
1069 this.context.router.history.push('/');