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,
105 userSettingsLoading: null,
106 deleteAccountLoading: null,
107 deleteAccountShowConfirm: false,
113 constructor(props: any, context: any) {
114 super(props, context);
116 this.state = this.emptyState;
117 this.handleSortChange = this.handleSortChange.bind(this);
118 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
121 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
125 this.state.user_id = Number(this.props.match.params.id);
126 this.state.username = this.props.match.params.username;
128 this.subscription = WebSocketService.Instance.subject
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 && (
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}
317 nodes={[{ comment: i.data as Comment }]}
318 admins={this.state.admins}
331 {this.state.comments.map(comment => (
333 nodes={[{ comment: comment }]}
334 admins={this.state.admins}
345 {this.state.posts.map(post => (
348 admins={this.state.admins}
358 let user = this.state.user;
361 <div class="card border-secondary mb-3">
362 <div class="card-body">
364 <ul class="list-inline mb-0">
365 <li className="list-inline-item">{user.name}</li>
367 <li className="list-inline-item badge badge-danger">
368 <T i18nKey="banned">#</T>
374 {i18n.t('joined')} <MomentTime data={user} />
376 <div class="table-responsive">
377 <table class="table table-bordered table-sm mt-2 mb-0">
381 i18nKey="number_of_points"
382 interpolation={{ count: user.post_score }}
389 i18nKey="number_of_posts"
390 interpolation={{ count: user.number_of_posts }}
399 i18nKey="number_of_points"
400 interpolation={{ count: user.comment_score }}
407 i18nKey="number_of_comments"
408 interpolation={{ count: user.number_of_comments }}
416 {this.isCurrentUser && (
418 class="btn btn-block btn-secondary mt-3"
419 onClick={linkEvent(this, this.handleLogoutClick)}
421 <T i18nKey="logout">#</T>
433 <div class="card border-secondary mb-3">
434 <div class="card-body">
436 <T i18nKey="settings">#</T>
438 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
439 <div class="form-group">
442 <T i18nKey="avatar">#</T>
444 <form class="d-inline">
446 htmlFor="file-upload"
447 class="pointer ml-4 text-muted small font-weight-bold"
453 this.state.userSettingsForm.avatar
454 ? this.state.userSettingsForm.avatar
455 : 'https://via.placeholder.com/300/000?text=Avatar'
457 class="rounded-circle"
463 accept="image/*,video/*"
466 disabled={!UserService.Instance.user}
467 onChange={linkEvent(this, this.handleImageUpload)}
472 <div class="form-group">
475 <T i18nKey="language">#</T>
478 value={this.state.userSettingsForm.lang}
481 this.handleUserSettingsLangChange
483 class="ml-2 custom-select custom-select-sm w-auto"
486 <T i18nKey="language">#</T>
488 <option value="browser">
489 <T i18nKey="browser_default">#</T>
491 <option disabled>──</option>
492 {languages.map(lang => (
493 <option value={lang.code}>{lang.name}</option>
498 <div class="form-group">
501 <T i18nKey="theme">#</T>
504 value={this.state.userSettingsForm.theme}
507 this.handleUserSettingsThemeChange
509 class="ml-2 custom-select custom-select-sm w-auto"
512 <T i18nKey="theme">#</T>
514 {themes.map(theme => (
515 <option value={theme}>{theme}</option>
520 <form className="form-group">
523 <T i18nKey="sort_type" class="mr-2">
528 type_={this.state.userSettingsForm.default_listing_type}
529 onChange={this.handleUserSettingsListingTypeChange}
533 <form className="form-group">
536 <T i18nKey="type" class="mr-2">
541 sort={this.state.userSettingsForm.default_sort_type}
542 onChange={this.handleUserSettingsSortTypeChange}
546 {WebSocketService.Instance.site.enable_nsfw && (
547 <div class="form-group">
549 <div class="form-check">
551 class="form-check-input"
553 checked={this.state.userSettingsForm.show_nsfw}
556 this.handleUserSettingsShowNsfwChange
559 <label class="form-check-label">
560 <T i18nKey="show_nsfw">#</T>
566 <div class="form-group">
570 class="btn btn-block btn-secondary mr-4"
572 {this.state.userSettingsLoading ? (
573 <svg class="icon icon-spinner spin">
574 <use xlinkHref="#icon-spinner"></use>
577 capitalizeFirstLetter(i18n.t('save'))
583 <div class="form-group mb-0">
586 class="btn btn-block btn-danger"
589 this.handleDeleteAccountShowConfirmToggle
592 <T i18nKey="delete_account">#</T>
594 {this.state.deleteAccountShowConfirm && (
596 <div class="my-2 alert alert-danger" role="alert">
597 <T i18nKey="delete_account_confirm">#</T>
601 value={this.state.deleteAccountForm.password}
604 this.handleDeleteAccountPasswordChange
606 class="form-control my-2"
609 class="btn btn-danger mr-4"
610 disabled={!this.state.deleteAccountForm.password}
611 onClick={linkEvent(this, this.handleDeleteAccount)}
613 {this.state.deleteAccountLoading ? (
614 <svg class="icon icon-spinner spin">
615 <use xlinkHref="#icon-spinner"></use>
618 capitalizeFirstLetter(i18n.t('delete'))
622 class="btn btn-secondary"
625 this.handleDeleteAccountShowConfirmToggle
628 <T i18nKey="cancel">#</T>
644 {this.state.moderates.length > 0 && (
645 <div class="card border-secondary mb-3">
646 <div class="card-body">
648 <T i18nKey="moderates">#</T>
650 <ul class="list-unstyled mb-0">
651 {this.state.moderates.map(community => (
653 <Link to={`/c/${community.community_name}`}>
654 {community.community_name}
669 {this.state.follows.length > 0 && (
670 <div class="card border-secondary mb-3">
671 <div class="card-body">
673 <T i18nKey="subscribed">#</T>
675 <ul class="list-unstyled mb-0">
676 {this.state.follows.map(community => (
678 <Link to={`/c/${community.community_name}`}>
679 {community.community_name}
694 {this.state.page > 1 && (
696 class="btn btn-sm btn-secondary mr-1"
697 onClick={linkEvent(this, this.prevPage)}
699 <T i18nKey="prev">#</T>
703 class="btn btn-sm btn-secondary"
704 onClick={linkEvent(this, this.nextPage)}
706 <T i18nKey="next">#</T>
713 let viewStr = View[this.state.view].toLowerCase();
714 let sortStr = SortType[this.state.sort].toLowerCase();
715 this.props.history.push(
716 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
735 let form: GetUserDetailsForm = {
736 user_id: this.state.user_id,
737 username: this.state.username,
738 sort: SortType[this.state.sort],
739 saved_only: this.state.view == View.Saved,
740 page: this.state.page,
743 WebSocketService.Instance.getUserDetails(form);
746 handleSortChange(val: SortType) {
747 this.state.sort = val;
749 this.setState(this.state);
754 handleViewChange(i: User, event: any) {
755 i.state.view = Number(event.target.value);
762 handleUserSettingsShowNsfwChange(i: User, event: any) {
763 i.state.userSettingsForm.show_nsfw = event.target.checked;
767 handleUserSettingsThemeChange(i: User, event: any) {
768 i.state.userSettingsForm.theme = event.target.value;
769 setTheme(event.target.value);
773 handleUserSettingsLangChange(i: User, event: any) {
774 i.state.userSettingsForm.lang = event.target.value;
775 i18n.changeLanguage(i.state.userSettingsForm.lang);
779 handleUserSettingsSortTypeChange(val: SortType) {
780 this.state.userSettingsForm.default_sort_type = val;
781 this.setState(this.state);
784 handleUserSettingsListingTypeChange(val: ListingType) {
785 this.state.userSettingsForm.default_listing_type = val;
786 this.setState(this.state);
789 handleImageUpload(i: User, event: any) {
790 event.preventDefault();
791 let file = event.target.files[0];
792 const imageUploadUrl = `/pictshare/api/upload.php`;
793 const formData = new FormData();
794 formData.append('file', file);
796 i.state.avatarLoading = true;
799 fetch(imageUploadUrl, {
803 .then(res => res.json())
805 let url = `${window.location.origin}/pictshare/${res.url}`;
806 if (res.filetype == 'mp4') {
809 i.state.userSettingsForm.avatar = url;
811 i.state.avatarLoading = false;
815 i.state.avatarLoading = false;
821 handleUserSettingsSubmit(i: User, event: any) {
822 event.preventDefault();
823 i.state.userSettingsLoading = true;
826 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
829 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
830 event.preventDefault();
831 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
835 handleDeleteAccountPasswordChange(i: User, event: any) {
836 i.state.deleteAccountForm.password = event.target.value;
840 handleLogoutClick(i: User) {
841 UserService.Instance.logout();
842 i.context.router.history.push('/');
845 handleDeleteAccount(i: User, event: any) {
846 event.preventDefault();
847 i.state.deleteAccountLoading = true;
850 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
853 parseMessage(msg: any) {
855 let op: UserOperation = msgOp(msg);
857 alert(i18n.t(msg.error));
858 this.state.deleteAccountLoading = false;
859 if (msg.error == 'couldnt_find_that_username_or_email') {
860 this.context.router.history.push('/');
862 this.setState(this.state);
864 } else if (op == UserOperation.GetUserDetails) {
865 let res: UserDetailsResponse = msg;
866 this.state.user = res.user;
867 this.state.comments = res.comments;
868 this.state.follows = res.follows;
869 this.state.moderates = res.moderates;
870 this.state.posts = res.posts;
871 this.state.admins = res.admins;
872 this.state.loading = false;
873 if (this.isCurrentUser) {
874 this.state.userSettingsForm.show_nsfw =
875 UserService.Instance.user.show_nsfw;
876 this.state.userSettingsForm.theme = UserService.Instance.user.theme
877 ? UserService.Instance.user.theme
879 this.state.userSettingsForm.default_sort_type =
880 UserService.Instance.user.default_sort_type;
881 this.state.userSettingsForm.default_listing_type =
882 UserService.Instance.user.default_listing_type;
883 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
884 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
886 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
887 window.scrollTo(0, 0);
888 this.setState(this.state);
889 } else if (op == UserOperation.EditComment) {
890 let res: CommentResponse = msg;
892 let found = this.state.comments.find(c => c.id == res.comment.id);
893 found.content = res.comment.content;
894 found.updated = res.comment.updated;
895 found.removed = res.comment.removed;
896 found.deleted = res.comment.deleted;
897 found.upvotes = res.comment.upvotes;
898 found.downvotes = res.comment.downvotes;
899 found.score = res.comment.score;
901 this.setState(this.state);
902 } else if (op == UserOperation.CreateComment) {
903 // let res: CommentResponse = msg;
904 alert(i18n.t('reply_sent'));
905 // this.state.comments.unshift(res.comment); // TODO do this right
906 // this.setState(this.state);
907 } else if (op == UserOperation.SaveComment) {
908 let res: CommentResponse = msg;
909 let found = this.state.comments.find(c => c.id == res.comment.id);
910 found.saved = res.comment.saved;
911 this.setState(this.state);
912 } else if (op == UserOperation.CreateCommentLike) {
913 let res: CommentResponse = msg;
914 let found: Comment = this.state.comments.find(
915 c => c.id === res.comment.id
917 found.score = res.comment.score;
918 found.upvotes = res.comment.upvotes;
919 found.downvotes = res.comment.downvotes;
920 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
921 this.setState(this.state);
922 } else if (op == UserOperation.BanUser) {
923 let res: BanUserResponse = msg;
925 .filter(c => c.creator_id == res.user.id)
926 .forEach(c => (c.banned = res.banned));
928 .filter(c => c.creator_id == res.user.id)
929 .forEach(c => (c.banned = res.banned));
930 this.setState(this.state);
931 } else if (op == UserOperation.AddAdmin) {
932 let res: AddAdminResponse = msg;
933 this.state.admins = res.admins;
934 this.setState(this.state);
935 } else if (op == UserOperation.SaveUserSettings) {
936 this.state = this.emptyState;
937 this.state.userSettingsLoading = false;
938 this.setState(this.state);
939 let res: LoginResponse = msg;
940 UserService.Instance.login(res);
941 } else if (op == UserOperation.DeleteAccount) {
942 this.state.deleteAccountLoading = false;
943 this.state.deleteAccountShowConfirm = false;
944 this.setState(this.state);
945 this.context.router.history.push('/');