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,
32 import { PostListing } from './post-listing';
33 import { SortSelect } from './sort-select';
34 import { ListingTypeSelect } from './listing-type-select';
35 import { CommentNodes } from './comment-nodes';
36 import { MomentTime } from './moment-time';
37 import { i18n } from '../i18next';
38 import { T } from 'inferno-i18next';
51 follows: Array<CommunityUser>;
52 moderates: Array<CommunityUser>;
53 comments: Array<Comment>;
56 admins: Array<UserView>;
61 avatarLoading: boolean;
62 userSettingsForm: UserSettingsForm;
63 userSettingsLoading: boolean;
64 deleteAccountLoading: boolean;
65 deleteAccountShowConfirm: boolean;
66 deleteAccountForm: DeleteAccountForm;
69 export class User extends Component<any, UserState> {
70 private subscription: Subscription;
71 private emptyState: UserState = {
77 number_of_posts: null,
79 number_of_comments: null,
93 view: this.getViewFromProps(this.props),
94 sort: this.getSortTypeFromProps(this.props),
95 page: this.getPageFromProps(this.props),
99 default_sort_type: null,
100 default_listing_type: null,
104 userSettingsLoading: null,
105 deleteAccountLoading: null,
106 deleteAccountShowConfirm: false,
112 constructor(props: any, context: any) {
113 super(props, context);
115 this.state = this.emptyState;
116 this.handleSortChange = this.handleSortChange.bind(this);
117 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
120 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
124 this.state.user_id = Number(this.props.match.params.id);
125 this.state.username = this.props.match.params.username;
127 this.subscription = WebSocketService.Instance.subject
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 && (
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}
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 => (
347 admins={this.state.admins}
357 let user = this.state.user;
360 <div class="card border-secondary mb-3">
361 <div class="card-body">
363 <ul class="list-inline mb-0">
364 <li className="list-inline-item">{user.name}</li>
366 <li className="list-inline-item badge badge-danger">
367 <T i18nKey="banned">#</T>
373 {i18n.t('joined')} <MomentTime data={user} />
375 <div class="table-responsive">
376 <table class="table table-bordered table-sm mt-2 mb-0">
380 i18nKey="number_of_points"
381 interpolation={{ count: user.post_score }}
388 i18nKey="number_of_posts"
389 interpolation={{ count: user.number_of_posts }}
398 i18nKey="number_of_points"
399 interpolation={{ count: user.comment_score }}
406 i18nKey="number_of_comments"
407 interpolation={{ count: user.number_of_comments }}
415 {this.isCurrentUser && (
417 class="btn btn-block btn-secondary mt-3"
418 onClick={linkEvent(this, this.handleLogoutClick)}
420 <T i18nKey="logout">#</T>
432 <div class="card border-secondary mb-3">
433 <div class="card-body">
435 <T i18nKey="settings">#</T>
437 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
438 <div class="form-group">
440 <T i18nKey="avatar">#</T>
442 <form class="d-inline">
444 htmlFor="file-upload"
445 class="pointer ml-4 text-muted small font-weight-bold"
451 this.state.userSettingsForm.avatar
452 ? this.state.userSettingsForm.avatar
453 : 'https://via.placeholder.com/300/000?text=Avatar'
455 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 <button type="submit" class="btn btn-block btn-secondary mr-4">
615 {this.state.userSettingsLoading ? (
616 <svg class="icon icon-spinner spin">
617 <use xlinkHref="#icon-spinner"></use>
620 capitalizeFirstLetter(i18n.t('save'))
625 <div class="form-group mb-0">
627 class="btn btn-block btn-danger"
630 this.handleDeleteAccountShowConfirmToggle
633 <T i18nKey="delete_account">#</T>
635 {this.state.deleteAccountShowConfirm && (
637 <div class="my-2 alert alert-danger" role="alert">
638 <T i18nKey="delete_account_confirm">#</T>
642 value={this.state.deleteAccountForm.password}
645 this.handleDeleteAccountPasswordChange
647 class="form-control my-2"
650 class="btn btn-danger mr-4"
651 disabled={!this.state.deleteAccountForm.password}
652 onClick={linkEvent(this, this.handleDeleteAccount)}
654 {this.state.deleteAccountLoading ? (
655 <svg class="icon icon-spinner spin">
656 <use xlinkHref="#icon-spinner"></use>
659 capitalizeFirstLetter(i18n.t('delete'))
663 class="btn btn-secondary"
666 this.handleDeleteAccountShowConfirmToggle
669 <T i18nKey="cancel">#</T>
684 {this.state.moderates.length > 0 && (
685 <div class="card border-secondary mb-3">
686 <div class="card-body">
688 <T i18nKey="moderates">#</T>
690 <ul class="list-unstyled mb-0">
691 {this.state.moderates.map(community => (
693 <Link to={`/c/${community.community_name}`}>
694 {community.community_name}
709 {this.state.follows.length > 0 && (
710 <div class="card border-secondary mb-3">
711 <div class="card-body">
713 <T i18nKey="subscribed">#</T>
715 <ul class="list-unstyled mb-0">
716 {this.state.follows.map(community => (
718 <Link to={`/c/${community.community_name}`}>
719 {community.community_name}
734 {this.state.page > 1 && (
736 class="btn btn-sm btn-secondary mr-1"
737 onClick={linkEvent(this, this.prevPage)}
739 <T i18nKey="prev">#</T>
743 class="btn btn-sm btn-secondary"
744 onClick={linkEvent(this, this.nextPage)}
746 <T i18nKey="next">#</T>
753 let viewStr = View[this.state.view].toLowerCase();
754 let sortStr = SortType[this.state.sort].toLowerCase();
755 this.props.history.push(
756 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
775 let form: GetUserDetailsForm = {
776 user_id: this.state.user_id,
777 username: this.state.username,
778 sort: SortType[this.state.sort],
779 saved_only: this.state.view == View.Saved,
780 page: this.state.page,
783 WebSocketService.Instance.getUserDetails(form);
786 handleSortChange(val: SortType) {
787 this.state.sort = val;
789 this.setState(this.state);
794 handleViewChange(i: User, event: any) {
795 i.state.view = Number(event.target.value);
802 handleUserSettingsShowNsfwChange(i: User, event: any) {
803 i.state.userSettingsForm.show_nsfw = event.target.checked;
807 handleUserSettingsThemeChange(i: User, event: any) {
808 i.state.userSettingsForm.theme = event.target.value;
809 setTheme(event.target.value);
813 handleUserSettingsLangChange(i: User, event: any) {
814 i.state.userSettingsForm.lang = event.target.value;
815 i18n.changeLanguage(i.state.userSettingsForm.lang);
819 handleUserSettingsSortTypeChange(val: SortType) {
820 this.state.userSettingsForm.default_sort_type = val;
821 this.setState(this.state);
824 handleUserSettingsListingTypeChange(val: ListingType) {
825 this.state.userSettingsForm.default_listing_type = val;
826 this.setState(this.state);
829 handleUserSettingsEmailChange(i: User, event: any) {
830 i.state.userSettingsForm.email = event.target.value;
831 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
832 i.state.userSettingsForm.email = undefined;
837 handleUserSettingsNewPasswordChange(i: User, event: any) {
838 i.state.userSettingsForm.new_password = event.target.value;
839 if (i.state.userSettingsForm.new_password == '') {
840 i.state.userSettingsForm.new_password = undefined;
845 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
846 i.state.userSettingsForm.new_password_verify = event.target.value;
847 if (i.state.userSettingsForm.new_password_verify == '') {
848 i.state.userSettingsForm.new_password_verify = undefined;
853 handleUserSettingsOldPasswordChange(i: User, event: any) {
854 i.state.userSettingsForm.old_password = event.target.value;
855 if (i.state.userSettingsForm.old_password == '') {
856 i.state.userSettingsForm.old_password = undefined;
861 handleImageUpload(i: User, event: any) {
862 event.preventDefault();
863 let file = event.target.files[0];
864 const imageUploadUrl = `/pictshare/api/upload.php`;
865 const formData = new FormData();
866 formData.append('file', file);
868 i.state.avatarLoading = true;
871 fetch(imageUploadUrl, {
875 .then(res => res.json())
877 let url = `${window.location.origin}/pictshare/${res.url}`;
878 if (res.filetype == 'mp4') {
881 i.state.userSettingsForm.avatar = url;
883 i.state.avatarLoading = false;
887 i.state.avatarLoading = false;
893 handleUserSettingsSubmit(i: User, event: any) {
894 event.preventDefault();
895 i.state.userSettingsLoading = true;
898 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
901 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
902 event.preventDefault();
903 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
907 handleDeleteAccountPasswordChange(i: User, event: any) {
908 i.state.deleteAccountForm.password = event.target.value;
912 handleLogoutClick(i: User) {
913 UserService.Instance.logout();
914 i.context.router.history.push('/');
917 handleDeleteAccount(i: User, event: any) {
918 event.preventDefault();
919 i.state.deleteAccountLoading = true;
922 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
925 parseMessage(msg: any) {
927 let op: UserOperation = msgOp(msg);
929 alert(i18n.t(msg.error));
930 this.state.deleteAccountLoading = false;
931 this.state.avatarLoading = false;
932 this.state.userSettingsLoading = false;
933 if (msg.error == 'couldnt_find_that_username_or_email') {
934 this.context.router.history.push('/');
936 this.setState(this.state);
938 } else if (op == UserOperation.GetUserDetails) {
939 let res: UserDetailsResponse = msg;
940 this.state.user = res.user;
941 this.state.comments = res.comments;
942 this.state.follows = res.follows;
943 this.state.moderates = res.moderates;
944 this.state.posts = res.posts;
945 this.state.admins = res.admins;
946 this.state.loading = false;
947 if (this.isCurrentUser) {
948 this.state.userSettingsForm.show_nsfw =
949 UserService.Instance.user.show_nsfw;
950 this.state.userSettingsForm.theme = UserService.Instance.user.theme
951 ? UserService.Instance.user.theme
953 this.state.userSettingsForm.default_sort_type =
954 UserService.Instance.user.default_sort_type;
955 this.state.userSettingsForm.default_listing_type =
956 UserService.Instance.user.default_listing_type;
957 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
958 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
959 this.state.userSettingsForm.email = this.state.user.email;
961 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
962 window.scrollTo(0, 0);
963 this.setState(this.state);
964 } else if (op == UserOperation.EditComment) {
965 let res: CommentResponse = msg;
967 let found = this.state.comments.find(c => c.id == res.comment.id);
968 found.content = res.comment.content;
969 found.updated = res.comment.updated;
970 found.removed = res.comment.removed;
971 found.deleted = res.comment.deleted;
972 found.upvotes = res.comment.upvotes;
973 found.downvotes = res.comment.downvotes;
974 found.score = res.comment.score;
976 this.setState(this.state);
977 } else if (op == UserOperation.CreateComment) {
978 // let res: CommentResponse = msg;
979 alert(i18n.t('reply_sent'));
980 // this.state.comments.unshift(res.comment); // TODO do this right
981 // this.setState(this.state);
982 } else if (op == UserOperation.SaveComment) {
983 let res: CommentResponse = msg;
984 let found = this.state.comments.find(c => c.id == res.comment.id);
985 found.saved = res.comment.saved;
986 this.setState(this.state);
987 } else if (op == UserOperation.CreateCommentLike) {
988 let res: CommentResponse = msg;
989 let found: Comment = this.state.comments.find(
990 c => c.id === res.comment.id
992 found.score = res.comment.score;
993 found.upvotes = res.comment.upvotes;
994 found.downvotes = res.comment.downvotes;
995 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
996 this.setState(this.state);
997 } else if (op == UserOperation.BanUser) {
998 let res: BanUserResponse = msg;
1000 .filter(c => c.creator_id == res.user.id)
1001 .forEach(c => (c.banned = res.banned));
1003 .filter(c => c.creator_id == res.user.id)
1004 .forEach(c => (c.banned = res.banned));
1005 this.setState(this.state);
1006 } else if (op == UserOperation.AddAdmin) {
1007 let res: AddAdminResponse = msg;
1008 this.state.admins = res.admins;
1009 this.setState(this.state);
1010 } else if (op == UserOperation.SaveUserSettings) {
1011 this.state = this.emptyState;
1012 this.state.userSettingsLoading = false;
1013 this.setState(this.state);
1014 let res: LoginResponse = msg;
1015 UserService.Instance.login(res);
1016 } else if (op == UserOperation.DeleteAccount) {
1017 this.state.deleteAccountLoading = false;
1018 this.state.deleteAccountShowConfirm = false;
1019 this.setState(this.state);
1020 this.context.router.history.push('/');