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 WebSocketJsonResponse,
22 } from '../interfaces';
23 import { WebSocketService, UserService } from '../services';
28 capitalizeFirstLetter,
34 import { PostListing } from './post-listing';
35 import { SortSelect } from './sort-select';
36 import { ListingTypeSelect } from './listing-type-select';
37 import { CommentNodes } from './comment-nodes';
38 import { MomentTime } from './moment-time';
39 import { i18n } from '../i18next';
40 import { T } from 'inferno-i18next';
53 follows: Array<CommunityUser>;
54 moderates: Array<CommunityUser>;
55 comments: Array<Comment>;
58 admins: Array<UserView>;
63 avatarLoading: boolean;
64 userSettingsForm: UserSettingsForm;
65 userSettingsLoading: boolean;
66 deleteAccountLoading: boolean;
67 deleteAccountShowConfirm: boolean;
68 deleteAccountForm: DeleteAccountForm;
71 export class User extends Component<any, UserState> {
72 private subscription: Subscription;
73 private emptyState: UserState = {
79 number_of_posts: null,
81 number_of_comments: null,
86 send_notifications_to_email: null,
97 view: this.getViewFromProps(this.props),
98 sort: this.getSortTypeFromProps(this.props),
99 page: this.getPageFromProps(this.props),
103 default_sort_type: null,
104 default_listing_type: null,
107 send_notifications_to_email: null,
110 userSettingsLoading: null,
111 deleteAccountLoading: null,
112 deleteAccountShowConfirm: false,
118 constructor(props: any, context: any) {
119 super(props, context);
121 this.state = this.emptyState;
122 this.handleSortChange = this.handleSortChange.bind(this);
123 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
126 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
130 this.state.user_id = Number(this.props.match.params.id);
131 this.state.username = this.props.match.params.username;
133 this.subscription = WebSocketService.Instance.subject
134 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
136 msg => this.parseMessage(msg),
137 err => console.error(err),
138 () => console.log('complete')
144 get isCurrentUser() {
146 UserService.Instance.user &&
147 UserService.Instance.user.id == this.state.user.id
151 getViewFromProps(props: any): View {
152 return props.match.params.view
153 ? View[capitalizeFirstLetter(props.match.params.view)]
157 getSortTypeFromProps(props: any): SortType {
158 return props.match.params.sort
159 ? routeSortTypeToEnum(props.match.params.sort)
163 getPageFromProps(props: any): number {
164 return props.match.params.page ? Number(props.match.params.page) : 1;
167 componentWillUnmount() {
168 this.subscription.unsubscribe();
171 // Necessary for back button for some reason
172 componentWillReceiveProps(nextProps: any) {
174 nextProps.history.action == 'POP' ||
175 nextProps.history.action == 'PUSH'
177 this.state.view = this.getViewFromProps(nextProps);
178 this.state.sort = this.getSortTypeFromProps(nextProps);
179 this.state.page = this.getPageFromProps(nextProps);
180 this.setState(this.state);
185 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
186 // Necessary if you are on a post and you click another post (same route)
188 lastProps.location.pathname.split('/')[2] !==
189 lastProps.history.location.pathname.split('/')[2]
191 // Couldnt get a refresh working. This does for now.
198 <div class="container">
199 {this.state.loading ? (
201 <svg class="icon icon-spinner spin">
202 <use xlinkHref="#icon-spinner"></use>
207 <div class="col-12 col-md-8">
209 {this.state.user.avatar && showAvatars() && (
213 src={this.state.user.avatar}
214 class="rounded-circle mr-2"
217 <span>/u/{this.state.user.name}</span>
220 {this.state.view == View.Overview && this.overview()}
221 {this.state.view == View.Comments && this.comments()}
222 {this.state.view == View.Posts && this.posts()}
223 {this.state.view == View.Saved && this.overview()}
226 <div class="col-12 col-md-4">
228 {this.isCurrentUser && this.userSettings()}
240 <div className="mb-2">
242 value={this.state.view}
243 onChange={linkEvent(this, this.handleViewChange)}
244 class="custom-select custom-select-sm w-auto"
247 <T i18nKey="view">#</T>
249 <option value={View.Overview}>
250 <T i18nKey="overview">#</T>
252 <option value={View.Comments}>
253 <T i18nKey="comments">#</T>
255 <option value={View.Posts}>
256 <T i18nKey="posts">#</T>
258 <option value={View.Saved}>
259 <T i18nKey="saved">#</T>
264 sort={this.state.sort}
265 onChange={this.handleSortChange}
270 href={`/feeds/u/${this.state.username}.xml?sort=${
271 SortType[this.state.sort]
275 <svg class="icon mx-2 text-muted small">
276 <use xlinkHref="#icon-rss">#</use>
284 let combined: Array<{ type_: string; data: Comment | Post }> = [];
285 let comments = this.state.comments.map(e => {
286 return { type_: 'comments', data: e };
288 let posts = this.state.posts.map(e => {
289 return { type_: 'posts', data: e };
292 combined.push(...comments);
293 combined.push(...posts);
296 if (this.state.sort == SortType.New) {
297 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
299 combined.sort((a, b) => b.data.score - a.data.score);
306 {i.type_ == 'posts' ? (
308 post={i.data as Post}
309 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 => (
346 admins={this.state.admins}
356 let user = this.state.user;
359 <div class="card border-secondary mb-3">
360 <div class="card-body">
362 <ul class="list-inline mb-0">
363 <li className="list-inline-item">{user.name}</li>
365 <li className="list-inline-item badge badge-danger">
366 <T i18nKey="banned">#</T>
372 {i18n.t('joined')} <MomentTime data={user} />
374 <div class="table-responsive">
375 <table class="table table-bordered table-sm mt-2 mb-0">
379 i18nKey="number_of_points"
380 interpolation={{ count: user.post_score }}
387 i18nKey="number_of_posts"
388 interpolation={{ count: user.number_of_posts }}
397 i18nKey="number_of_points"
398 interpolation={{ count: user.comment_score }}
405 i18nKey="number_of_comments"
406 interpolation={{ count: user.number_of_comments }}
414 {this.isCurrentUser && (
416 class="btn btn-block btn-secondary mt-3"
417 onClick={linkEvent(this, this.handleLogoutClick)}
419 <T i18nKey="logout">#</T>
431 <div class="card border-secondary mb-3">
432 <div class="card-body">
434 <T i18nKey="settings">#</T>
436 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
437 <div class="form-group">
439 <T i18nKey="avatar">#</T>
441 <form class="d-inline">
443 htmlFor="file-upload"
444 class="pointer ml-4 text-muted small font-weight-bold"
446 {!this.state.userSettingsForm.avatar ? (
447 <span class="btn btn-sm btn-secondary">
448 <T i18nKey="upload_avatar">#</T>
454 src={this.state.userSettingsForm.avatar}
455 class="rounded-circle"
462 accept="image/*,video/*"
465 disabled={!UserService.Instance.user}
466 onChange={linkEvent(this, this.handleImageUpload)}
470 <div class="form-group">
472 <T i18nKey="language">#</T>
475 value={this.state.userSettingsForm.lang}
476 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
477 class="ml-2 custom-select custom-select-sm w-auto"
480 <T i18nKey="language">#</T>
482 <option value="browser">
483 <T i18nKey="browser_default">#</T>
485 <option disabled>──</option>
486 {languages.map(lang => (
487 <option value={lang.code}>{lang.name}</option>
491 <div class="form-group">
493 <T i18nKey="theme">#</T>
496 value={this.state.userSettingsForm.theme}
497 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
498 class="ml-2 custom-select custom-select-sm w-auto"
501 <T i18nKey="theme">#</T>
503 {themes.map(theme => (
504 <option value={theme}>{theme}</option>
508 <form className="form-group">
510 <T i18nKey="sort_type" class="mr-2">
515 type_={this.state.userSettingsForm.default_listing_type}
516 onChange={this.handleUserSettingsListingTypeChange}
519 <form className="form-group">
521 <T i18nKey="type" class="mr-2">
526 sort={this.state.userSettingsForm.default_sort_type}
527 onChange={this.handleUserSettingsSortTypeChange}
530 <div class="form-group row">
531 <label class="col-lg-3 col-form-label">
532 <T i18nKey="email">#</T>
534 <div class="col-lg-9">
538 placeholder={i18n.t('optional')}
539 value={this.state.userSettingsForm.email}
542 this.handleUserSettingsEmailChange
548 <div class="form-group row">
549 <label class="col-lg-5 col-form-label">
550 <T i18nKey="new_password">#</T>
552 <div class="col-lg-7">
556 value={this.state.userSettingsForm.new_password}
559 this.handleUserSettingsNewPasswordChange
564 <div class="form-group row">
565 <label class="col-lg-5 col-form-label">
566 <T i18nKey="verify_password">#</T>
568 <div class="col-lg-7">
572 value={this.state.userSettingsForm.new_password_verify}
575 this.handleUserSettingsNewPasswordVerifyChange
580 <div class="form-group row">
581 <label class="col-lg-5 col-form-label">
582 <T i18nKey="old_password">#</T>
584 <div class="col-lg-7">
588 value={this.state.userSettingsForm.old_password}
591 this.handleUserSettingsOldPasswordChange
596 {WebSocketService.Instance.site.enable_nsfw && (
597 <div class="form-group">
598 <div class="form-check">
600 class="form-check-input"
602 checked={this.state.userSettingsForm.show_nsfw}
605 this.handleUserSettingsShowNsfwChange
608 <label class="form-check-label">
609 <T i18nKey="show_nsfw">#</T>
614 <div class="form-group">
615 <div class="form-check">
617 class="form-check-input"
619 checked={this.state.userSettingsForm.show_avatars}
622 this.handleUserSettingsShowAvatarsChange
625 <label class="form-check-label">
626 <T i18nKey="show_avatars">#</T>
630 <div class="form-group">
631 <div class="form-check">
633 class="form-check-input"
635 disabled={!this.state.user.email}
637 this.state.userSettingsForm.send_notifications_to_email
641 this.handleUserSettingsSendNotificationsToEmailChange
644 <label class="form-check-label">
645 <T i18nKey="send_notifications_to_email">#</T>
649 <div class="form-group">
650 <button type="submit" class="btn btn-block btn-secondary mr-4">
651 {this.state.userSettingsLoading ? (
652 <svg class="icon icon-spinner spin">
653 <use xlinkHref="#icon-spinner"></use>
656 capitalizeFirstLetter(i18n.t('save'))
661 <div class="form-group mb-0">
663 class="btn btn-block btn-danger"
666 this.handleDeleteAccountShowConfirmToggle
669 <T i18nKey="delete_account">#</T>
671 {this.state.deleteAccountShowConfirm && (
673 <div class="my-2 alert alert-danger" role="alert">
674 <T i18nKey="delete_account_confirm">#</T>
678 value={this.state.deleteAccountForm.password}
681 this.handleDeleteAccountPasswordChange
683 class="form-control my-2"
686 class="btn btn-danger mr-4"
687 disabled={!this.state.deleteAccountForm.password}
688 onClick={linkEvent(this, this.handleDeleteAccount)}
690 {this.state.deleteAccountLoading ? (
691 <svg class="icon icon-spinner spin">
692 <use xlinkHref="#icon-spinner"></use>
695 capitalizeFirstLetter(i18n.t('delete'))
699 class="btn btn-secondary"
702 this.handleDeleteAccountShowConfirmToggle
705 <T i18nKey="cancel">#</T>
720 {this.state.moderates.length > 0 && (
721 <div class="card border-secondary mb-3">
722 <div class="card-body">
724 <T i18nKey="moderates">#</T>
726 <ul class="list-unstyled mb-0">
727 {this.state.moderates.map(community => (
729 <Link to={`/c/${community.community_name}`}>
730 {community.community_name}
745 {this.state.follows.length > 0 && (
746 <div class="card border-secondary mb-3">
747 <div class="card-body">
749 <T i18nKey="subscribed">#</T>
751 <ul class="list-unstyled mb-0">
752 {this.state.follows.map(community => (
754 <Link to={`/c/${community.community_name}`}>
755 {community.community_name}
770 {this.state.page > 1 && (
772 class="btn btn-sm btn-secondary mr-1"
773 onClick={linkEvent(this, this.prevPage)}
775 <T i18nKey="prev">#</T>
779 class="btn btn-sm btn-secondary"
780 onClick={linkEvent(this, this.nextPage)}
782 <T i18nKey="next">#</T>
789 let viewStr = View[this.state.view].toLowerCase();
790 let sortStr = SortType[this.state.sort].toLowerCase();
791 this.props.history.push(
792 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
811 let form: GetUserDetailsForm = {
812 user_id: this.state.user_id,
813 username: this.state.username,
814 sort: SortType[this.state.sort],
815 saved_only: this.state.view == View.Saved,
816 page: this.state.page,
819 WebSocketService.Instance.getUserDetails(form);
822 handleSortChange(val: SortType) {
823 this.state.sort = val;
825 this.setState(this.state);
830 handleViewChange(i: User, event: any) {
831 i.state.view = Number(event.target.value);
838 handleUserSettingsShowNsfwChange(i: User, event: any) {
839 i.state.userSettingsForm.show_nsfw = event.target.checked;
843 handleUserSettingsShowAvatarsChange(i: User, event: any) {
844 i.state.userSettingsForm.show_avatars = event.target.checked;
845 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
849 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
850 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
854 handleUserSettingsThemeChange(i: User, event: any) {
855 i.state.userSettingsForm.theme = event.target.value;
856 setTheme(event.target.value);
860 handleUserSettingsLangChange(i: User, event: any) {
861 i.state.userSettingsForm.lang = event.target.value;
862 i18n.changeLanguage(i.state.userSettingsForm.lang);
866 handleUserSettingsSortTypeChange(val: SortType) {
867 this.state.userSettingsForm.default_sort_type = val;
868 this.setState(this.state);
871 handleUserSettingsListingTypeChange(val: ListingType) {
872 this.state.userSettingsForm.default_listing_type = val;
873 this.setState(this.state);
876 handleUserSettingsEmailChange(i: User, event: any) {
877 i.state.userSettingsForm.email = event.target.value;
878 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
879 i.state.userSettingsForm.email = undefined;
884 handleUserSettingsNewPasswordChange(i: User, event: any) {
885 i.state.userSettingsForm.new_password = event.target.value;
886 if (i.state.userSettingsForm.new_password == '') {
887 i.state.userSettingsForm.new_password = undefined;
892 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
893 i.state.userSettingsForm.new_password_verify = event.target.value;
894 if (i.state.userSettingsForm.new_password_verify == '') {
895 i.state.userSettingsForm.new_password_verify = undefined;
900 handleUserSettingsOldPasswordChange(i: User, event: any) {
901 i.state.userSettingsForm.old_password = event.target.value;
902 if (i.state.userSettingsForm.old_password == '') {
903 i.state.userSettingsForm.old_password = undefined;
908 handleImageUpload(i: User, event: any) {
909 event.preventDefault();
910 let file = event.target.files[0];
911 const imageUploadUrl = `/pictshare/api/upload.php`;
912 const formData = new FormData();
913 formData.append('file', file);
915 i.state.avatarLoading = true;
918 fetch(imageUploadUrl, {
922 .then(res => res.json())
924 let url = `${window.location.origin}/pictshare/${res.url}`;
925 if (res.filetype == 'mp4') {
928 i.state.userSettingsForm.avatar = url;
930 i.state.avatarLoading = false;
934 i.state.avatarLoading = false;
940 handleUserSettingsSubmit(i: User, event: any) {
941 event.preventDefault();
942 i.state.userSettingsLoading = true;
945 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
948 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
949 event.preventDefault();
950 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
954 handleDeleteAccountPasswordChange(i: User, event: any) {
955 i.state.deleteAccountForm.password = event.target.value;
959 handleLogoutClick(i: User) {
960 UserService.Instance.logout();
961 i.context.router.history.push('/');
964 handleDeleteAccount(i: User, event: any) {
965 event.preventDefault();
966 i.state.deleteAccountLoading = true;
969 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
972 parseMessage(msg: WebSocketJsonResponse) {
974 let res = wsJsonToRes(msg);
976 alert(i18n.t(res.error));
977 this.state.deleteAccountLoading = false;
978 this.state.avatarLoading = false;
979 this.state.userSettingsLoading = false;
980 if (res.error == 'couldnt_find_that_username_or_email') {
981 this.context.router.history.push('/');
983 this.setState(this.state);
985 } else if (res.op == UserOperation.GetUserDetails) {
986 let data = res.data as UserDetailsResponse;
987 this.state.user = data.user;
988 this.state.comments = data.comments;
989 this.state.follows = data.follows;
990 this.state.moderates = data.moderates;
991 this.state.posts = data.posts;
992 this.state.admins = data.admins;
993 this.state.loading = false;
994 if (this.isCurrentUser) {
995 this.state.userSettingsForm.show_nsfw =
996 UserService.Instance.user.show_nsfw;
997 this.state.userSettingsForm.theme = UserService.Instance.user.theme
998 ? UserService.Instance.user.theme
1000 this.state.userSettingsForm.default_sort_type =
1001 UserService.Instance.user.default_sort_type;
1002 this.state.userSettingsForm.default_listing_type =
1003 UserService.Instance.user.default_listing_type;
1004 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1005 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1006 this.state.userSettingsForm.email = this.state.user.email;
1007 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1008 this.state.userSettingsForm.show_avatars =
1009 UserService.Instance.user.show_avatars;
1011 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1012 window.scrollTo(0, 0);
1013 this.setState(this.state);
1014 } else if (res.op == UserOperation.EditComment) {
1015 let data = res.data as CommentResponse;
1017 let found = this.state.comments.find(c => c.id == data.comment.id);
1018 found.content = data.comment.content;
1019 found.updated = data.comment.updated;
1020 found.removed = data.comment.removed;
1021 found.deleted = data.comment.deleted;
1022 found.upvotes = data.comment.upvotes;
1023 found.downvotes = data.comment.downvotes;
1024 found.score = data.comment.score;
1026 this.setState(this.state);
1027 } else if (res.op == UserOperation.CreateComment) {
1028 // let res: CommentResponse = msg;
1029 alert(i18n.t('reply_sent'));
1030 // this.state.comments.unshift(res.comment); // TODO do this right
1031 // this.setState(this.state);
1032 } else if (res.op == UserOperation.SaveComment) {
1033 let data = res.data as CommentResponse;
1034 let found = this.state.comments.find(c => c.id == data.comment.id);
1035 found.saved = data.comment.saved;
1036 this.setState(this.state);
1037 } else if (res.op == UserOperation.CreateCommentLike) {
1038 let data = res.data as CommentResponse;
1039 let found: Comment = this.state.comments.find(
1040 c => c.id === data.comment.id
1042 found.score = data.comment.score;
1043 found.upvotes = data.comment.upvotes;
1044 found.downvotes = data.comment.downvotes;
1045 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1046 this.setState(this.state);
1047 } else if (res.op == UserOperation.BanUser) {
1048 let data = res.data as BanUserResponse;
1050 .filter(c => c.creator_id == data.user.id)
1051 .forEach(c => (c.banned = data.banned));
1053 .filter(c => c.creator_id == data.user.id)
1054 .forEach(c => (c.banned = data.banned));
1055 this.setState(this.state);
1056 } else if (res.op == UserOperation.AddAdmin) {
1057 let data = res.data as AddAdminResponse;
1058 this.state.admins = data.admins;
1059 this.setState(this.state);
1060 } else if (res.op == UserOperation.SaveUserSettings) {
1061 let data = res.data as LoginResponse;
1062 this.state = this.emptyState;
1063 this.state.userSettingsLoading = false;
1064 this.setState(this.state);
1065 UserService.Instance.login(data);
1066 } else if (res.op == UserOperation.DeleteAccount) {
1067 this.state.deleteAccountLoading = false;
1068 this.state.deleteAccountShowConfirm = false;
1069 this.setState(this.state);
1070 this.context.router.history.push('/');