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';
22 WebSocketJsonResponse,
23 } from '../interfaces';
24 import { WebSocketService, UserService } from '../services';
29 capitalizeFirstLetter,
36 import { PostListing } from './post-listing';
37 import { SortSelect } from './sort-select';
38 import { ListingTypeSelect } from './listing-type-select';
39 import { CommentNodes } from './comment-nodes';
40 import { MomentTime } from './moment-time';
41 import { i18n } from '../i18next';
54 follows: Array<CommunityUser>;
55 moderates: Array<CommunityUser>;
56 comments: Array<Comment>;
59 admins: Array<UserView>;
64 avatarLoading: boolean;
65 userSettingsForm: UserSettingsForm;
66 userSettingsLoading: boolean;
67 deleteAccountLoading: boolean;
68 deleteAccountShowConfirm: boolean;
69 deleteAccountForm: DeleteAccountForm;
72 export class User extends Component<any, UserState> {
73 private subscription: Subscription;
74 private emptyState: UserState = {
80 number_of_posts: null,
82 number_of_comments: null,
87 send_notifications_to_email: null,
98 view: this.getViewFromProps(this.props),
99 sort: this.getSortTypeFromProps(this.props),
100 page: this.getPageFromProps(this.props),
104 default_sort_type: null,
105 default_listing_type: null,
108 send_notifications_to_email: null,
111 userSettingsLoading: null,
112 deleteAccountLoading: null,
113 deleteAccountShowConfirm: false,
119 constructor(props: any, context: any) {
120 super(props, context);
122 this.state = this.emptyState;
123 this.handleSortChange = this.handleSortChange.bind(this);
124 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
127 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
131 this.state.user_id = Number(this.props.match.params.id);
132 this.state.username = this.props.match.params.username;
134 this.subscription = WebSocketService.Instance.subject
135 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
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 && showAvatars() && (
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"
247 <option disabled>{i18n.t('view')}</option>
248 <option value={View.Overview}>{i18n.t('overview')}</option>
249 <option value={View.Comments}>{i18n.t('comments')}</option>
250 <option value={View.Posts}>{i18n.t('posts')}</option>
251 <option value={View.Saved}>{i18n.t('saved')}</option>
255 sort={this.state.sort}
256 onChange={this.handleSortChange}
261 href={`/feeds/u/${this.state.username}.xml?sort=${
262 SortType[this.state.sort]
266 <svg class="icon mx-2 text-muted small">
267 <use xlinkHref="#icon-rss">#</use>
275 let combined: Array<{ type_: string; data: Comment | Post }> = [];
276 let comments = this.state.comments.map(e => {
277 return { type_: 'comments', data: e };
279 let posts = this.state.posts.map(e => {
280 return { type_: 'posts', data: e };
283 combined.push(...comments);
284 combined.push(...posts);
287 if (this.state.sort == SortType.New) {
288 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
290 combined.sort((a, b) => b.data.score - a.data.score);
297 {i.type_ == 'posts' ? (
299 post={i.data as Post}
300 admins={this.state.admins}
305 nodes={[{ comment: i.data as Comment }]}
306 admins={this.state.admins}
319 {this.state.comments.map(comment => (
321 nodes={[{ comment: comment }]}
322 admins={this.state.admins}
333 {this.state.posts.map(post => (
334 <PostListing post={post} admins={this.state.admins} showCommunity />
341 let user = this.state.user;
344 <div class="card border-secondary mb-3">
345 <div class="card-body">
347 <ul class="list-inline mb-0">
348 <li className="list-inline-item">{user.name}</li>
350 <li className="list-inline-item badge badge-danger">
357 {i18n.t('joined')} <MomentTime data={user} />
360 {i18n.t('total_number_of_points', { count: (user.post_score + user.comment_score) })}
362 <div class="table-responsive">
363 <table class="table table-bordered table-sm mt-2 mb-0">
366 {i18n.t('number_of_points', { count: user.post_score })}
369 {i18n.t('number_of_posts', { count: user.number_of_posts })}
374 {i18n.t('number_of_points', { count: user.comment_score })}
377 {i18n.t('number_of_comments', {
378 count: user.number_of_comments,
384 {this.isCurrentUser ? (
386 class="btn btn-block btn-secondary mt-3"
387 onClick={linkEvent(this, this.handleLogoutClick)}
394 className={`btn btn-block btn-secondary mt-3 ${!this.state
395 .user.matrix_user_id && 'disabled'}`}
397 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
399 {i18n.t('send_secure_message')}
402 class="btn btn-block btn-secondary mt-3"
403 to={`/create_private_message?recipient_id=${this.state.user.id}`}
405 {i18n.t('send_message')}
418 <div class="card border-secondary mb-3">
419 <div class="card-body">
420 <h5>{i18n.t('settings')}</h5>
421 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
422 <div class="form-group">
423 <label>{i18n.t('avatar')}</label>
424 <form class="d-inline">
426 htmlFor="file-upload"
427 class="pointer ml-4 text-muted small font-weight-bold"
429 {!this.state.userSettingsForm.avatar ? (
430 <span class="btn btn-sm btn-secondary">
431 {i18n.t('upload_avatar')}
437 src={this.state.userSettingsForm.avatar}
438 class="rounded-circle"
445 accept="image/*,video/*"
448 disabled={!UserService.Instance.user}
449 onChange={linkEvent(this, this.handleImageUpload)}
453 <div class="form-group">
454 <label>{i18n.t('language')}</label>
456 value={this.state.userSettingsForm.lang}
457 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
458 class="ml-2 custom-select custom-select-sm w-auto"
460 <option disabled>{i18n.t('language')}</option>
461 <option value="browser">{i18n.t('browser_default')}</option>
462 <option disabled>──</option>
463 {languages.map(lang => (
464 <option value={lang.code}>{lang.name}</option>
468 <div class="form-group">
469 <label>{i18n.t('theme')}</label>
471 value={this.state.userSettingsForm.theme}
472 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
473 class="ml-2 custom-select custom-select-sm w-auto"
475 <option disabled>{i18n.t('theme')}</option>
476 {themes.map(theme => (
477 <option value={theme}>{theme}</option>
481 <form className="form-group">
483 <div class="mr-2">{i18n.t('sort_type')}</div>
486 type_={this.state.userSettingsForm.default_listing_type}
487 onChange={this.handleUserSettingsListingTypeChange}
490 <form className="form-group">
492 <div class="mr-2">{i18n.t('type')}</div>
495 sort={this.state.userSettingsForm.default_sort_type}
496 onChange={this.handleUserSettingsSortTypeChange}
499 <div class="form-group row">
500 <label class="col-lg-3 col-form-label" htmlFor="user-email">
503 <div class="col-lg-9">
508 placeholder={i18n.t('optional')}
509 value={this.state.userSettingsForm.email}
512 this.handleUserSettingsEmailChange
518 <div class="form-group row">
519 <label class="col-lg-5 col-form-label">
520 <a href="https://about.riot.im/" target="_blank">
521 {i18n.t('matrix_user_id')}
524 <div class="col-lg-7">
528 placeholder="@user:example.com"
529 value={this.state.userSettingsForm.matrix_user_id}
532 this.handleUserSettingsMatrixUserIdChange
538 <div class="form-group row">
539 <label class="col-lg-5 col-form-label" htmlFor="user-password">
540 {i18n.t('new_password')}
542 <div class="col-lg-7">
547 value={this.state.userSettingsForm.new_password}
550 this.handleUserSettingsNewPasswordChange
555 <div class="form-group row">
557 class="col-lg-5 col-form-label"
558 htmlFor="user-verify-password"
560 {i18n.t('verify_password')}
562 <div class="col-lg-7">
565 id="user-verify-password"
567 value={this.state.userSettingsForm.new_password_verify}
570 this.handleUserSettingsNewPasswordVerifyChange
575 <div class="form-group row">
577 class="col-lg-5 col-form-label"
578 htmlFor="user-old-password"
580 {i18n.t('old_password')}
582 <div class="col-lg-7">
585 id="user-old-password"
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"
602 checked={this.state.userSettingsForm.show_nsfw}
605 this.handleUserSettingsShowNsfwChange
608 <label class="form-check-label" htmlFor="user-show-nsfw">
609 {i18n.t('show_nsfw')}
614 <div class="form-group">
615 <div class="form-check">
617 class="form-check-input"
618 id="user-show-avatars"
620 checked={this.state.userSettingsForm.show_avatars}
623 this.handleUserSettingsShowAvatarsChange
626 <label class="form-check-label" htmlFor="user-show-avatars">
627 {i18n.t('show_avatars')}
631 <div class="form-group">
632 <div class="form-check">
634 class="form-check-input"
635 id="user-send-notifications-to-email"
637 disabled={!this.state.user.email}
639 this.state.userSettingsForm.send_notifications_to_email
643 this.handleUserSettingsSendNotificationsToEmailChange
647 class="form-check-label"
648 htmlFor="user-send-notifications-to-email"
650 {i18n.t('send_notifications_to_email')}
654 <div class="form-group">
655 <button type="submit" class="btn btn-block btn-secondary mr-4">
656 {this.state.userSettingsLoading ? (
657 <svg class="icon icon-spinner spin">
658 <use xlinkHref="#icon-spinner"></use>
661 capitalizeFirstLetter(i18n.t('save'))
666 <div class="form-group mb-0">
668 class="btn btn-block btn-danger"
671 this.handleDeleteAccountShowConfirmToggle
674 {i18n.t('delete_account')}
676 {this.state.deleteAccountShowConfirm && (
678 <div class="my-2 alert alert-danger" role="alert">
679 {i18n.t('delete_account_confirm')}
683 value={this.state.deleteAccountForm.password}
686 this.handleDeleteAccountPasswordChange
688 class="form-control my-2"
691 class="btn btn-danger mr-4"
692 disabled={!this.state.deleteAccountForm.password}
693 onClick={linkEvent(this, this.handleDeleteAccount)}
695 {this.state.deleteAccountLoading ? (
696 <svg class="icon icon-spinner spin">
697 <use xlinkHref="#icon-spinner"></use>
700 capitalizeFirstLetter(i18n.t('delete'))
704 class="btn btn-secondary"
707 this.handleDeleteAccountShowConfirmToggle
725 {this.state.moderates.length > 0 && (
726 <div class="card border-secondary mb-3">
727 <div class="card-body">
728 <h5>{i18n.t('moderates')}</h5>
729 <ul class="list-unstyled mb-0">
730 {this.state.moderates.map(community => (
732 <Link to={`/c/${community.community_name}`}>
733 {community.community_name}
748 {this.state.follows.length > 0 && (
749 <div class="card border-secondary mb-3">
750 <div class="card-body">
751 <h5>{i18n.t('subscribed')}</h5>
752 <ul class="list-unstyled mb-0">
753 {this.state.follows.map(community => (
755 <Link to={`/c/${community.community_name}`}>
756 {community.community_name}
771 {this.state.page > 1 && (
773 class="btn btn-sm btn-secondary mr-1"
774 onClick={linkEvent(this, this.prevPage)}
780 class="btn btn-sm btn-secondary"
781 onClick={linkEvent(this, this.nextPage)}
790 let viewStr = View[this.state.view].toLowerCase();
791 let sortStr = SortType[this.state.sort].toLowerCase();
792 this.props.history.push(
793 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
812 let form: GetUserDetailsForm = {
813 user_id: this.state.user_id,
814 username: this.state.username,
815 sort: SortType[this.state.sort],
816 saved_only: this.state.view == View.Saved,
817 page: this.state.page,
820 WebSocketService.Instance.getUserDetails(form);
823 handleSortChange(val: SortType) {
824 this.state.sort = val;
826 this.setState(this.state);
831 handleViewChange(i: User, event: any) {
832 i.state.view = Number(event.target.value);
839 handleUserSettingsShowNsfwChange(i: User, event: any) {
840 i.state.userSettingsForm.show_nsfw = event.target.checked;
844 handleUserSettingsShowAvatarsChange(i: User, event: any) {
845 i.state.userSettingsForm.show_avatars = event.target.checked;
846 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
850 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
851 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
855 handleUserSettingsThemeChange(i: User, event: any) {
856 i.state.userSettingsForm.theme = event.target.value;
857 setTheme(event.target.value);
861 handleUserSettingsLangChange(i: User, event: any) {
862 i.state.userSettingsForm.lang = event.target.value;
863 i18n.changeLanguage(i.state.userSettingsForm.lang);
867 handleUserSettingsSortTypeChange(val: SortType) {
868 this.state.userSettingsForm.default_sort_type = val;
869 this.setState(this.state);
872 handleUserSettingsListingTypeChange(val: ListingType) {
873 this.state.userSettingsForm.default_listing_type = val;
874 this.setState(this.state);
877 handleUserSettingsEmailChange(i: User, event: any) {
878 i.state.userSettingsForm.email = event.target.value;
879 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
880 i.state.userSettingsForm.email = undefined;
885 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
886 i.state.userSettingsForm.matrix_user_id = event.target.value;
888 i.state.userSettingsForm.matrix_user_id == '' &&
889 !i.state.user.matrix_user_id
891 i.state.userSettingsForm.matrix_user_id = undefined;
896 handleUserSettingsNewPasswordChange(i: User, event: any) {
897 i.state.userSettingsForm.new_password = event.target.value;
898 if (i.state.userSettingsForm.new_password == '') {
899 i.state.userSettingsForm.new_password = undefined;
904 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
905 i.state.userSettingsForm.new_password_verify = event.target.value;
906 if (i.state.userSettingsForm.new_password_verify == '') {
907 i.state.userSettingsForm.new_password_verify = undefined;
912 handleUserSettingsOldPasswordChange(i: User, event: any) {
913 i.state.userSettingsForm.old_password = event.target.value;
914 if (i.state.userSettingsForm.old_password == '') {
915 i.state.userSettingsForm.old_password = undefined;
920 handleImageUpload(i: User, event: any) {
921 event.preventDefault();
922 let file = event.target.files[0];
923 const imageUploadUrl = `/pictshare/api/upload.php`;
924 const formData = new FormData();
925 formData.append('file', file);
927 i.state.avatarLoading = true;
930 fetch(imageUploadUrl, {
934 .then(res => res.json())
936 let url = `${window.location.origin}/pictshare/${res.url}`;
937 if (res.filetype == 'mp4') {
940 i.state.userSettingsForm.avatar = url;
942 i.state.avatarLoading = false;
946 i.state.avatarLoading = false;
948 toast(error, 'danger');
952 handleUserSettingsSubmit(i: User, event: any) {
953 event.preventDefault();
954 i.state.userSettingsLoading = true;
957 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
960 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
961 event.preventDefault();
962 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
966 handleDeleteAccountPasswordChange(i: User, event: any) {
967 i.state.deleteAccountForm.password = event.target.value;
971 handleLogoutClick(i: User) {
972 UserService.Instance.logout();
973 i.context.router.history.push('/');
976 handleDeleteAccount(i: User, event: any) {
977 event.preventDefault();
978 i.state.deleteAccountLoading = true;
981 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
984 parseMessage(msg: WebSocketJsonResponse) {
986 let res = wsJsonToRes(msg);
988 toast(i18n.t(msg.error), 'danger');
989 this.state.deleteAccountLoading = false;
990 this.state.avatarLoading = false;
991 this.state.userSettingsLoading = false;
992 if (msg.error == 'couldnt_find_that_username_or_email') {
993 this.context.router.history.push('/');
995 this.setState(this.state);
997 } else if (res.op == UserOperation.GetUserDetails) {
998 let data = res.data as UserDetailsResponse;
999 this.state.user = data.user;
1000 this.state.comments = data.comments;
1001 this.state.follows = data.follows;
1002 this.state.moderates = data.moderates;
1003 this.state.posts = data.posts;
1004 this.state.admins = data.admins;
1005 this.state.loading = false;
1006 if (this.isCurrentUser) {
1007 this.state.userSettingsForm.show_nsfw =
1008 UserService.Instance.user.show_nsfw;
1009 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1010 ? UserService.Instance.user.theme
1012 this.state.userSettingsForm.default_sort_type =
1013 UserService.Instance.user.default_sort_type;
1014 this.state.userSettingsForm.default_listing_type =
1015 UserService.Instance.user.default_listing_type;
1016 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1017 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1018 this.state.userSettingsForm.email = this.state.user.email;
1019 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1020 this.state.userSettingsForm.show_avatars =
1021 UserService.Instance.user.show_avatars;
1022 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1024 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1025 window.scrollTo(0, 0);
1026 this.setState(this.state);
1027 } else if (res.op == UserOperation.EditComment) {
1028 let data = res.data as CommentResponse;
1030 let found = this.state.comments.find(c => c.id == data.comment.id);
1031 found.content = data.comment.content;
1032 found.updated = data.comment.updated;
1033 found.removed = data.comment.removed;
1034 found.deleted = data.comment.deleted;
1035 found.upvotes = data.comment.upvotes;
1036 found.downvotes = data.comment.downvotes;
1037 found.score = data.comment.score;
1039 this.setState(this.state);
1040 } else if (res.op == UserOperation.CreateComment) {
1041 // let res: CommentResponse = msg;
1042 toast(i18n.t('reply_sent'));
1043 // this.state.comments.unshift(res.comment); // TODO do this right
1044 // this.setState(this.state);
1045 } else if (res.op == UserOperation.SaveComment) {
1046 let data = res.data as CommentResponse;
1047 let found = this.state.comments.find(c => c.id == data.comment.id);
1048 found.saved = data.comment.saved;
1049 this.setState(this.state);
1050 } else if (res.op == UserOperation.CreateCommentLike) {
1051 let data = res.data as CommentResponse;
1052 let found: Comment = this.state.comments.find(
1053 c => c.id === data.comment.id
1055 found.score = data.comment.score;
1056 found.upvotes = data.comment.upvotes;
1057 found.downvotes = data.comment.downvotes;
1058 if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
1059 this.setState(this.state);
1060 } else if (res.op == UserOperation.CreatePostLike) {
1061 let data = res.data as PostResponse;
1062 let found = this.state.posts.find(c => c.id == data.post.id);
1063 found.my_vote = data.post.my_vote;
1064 found.score = data.post.score;
1065 found.upvotes = data.post.upvotes;
1066 found.downvotes = data.post.downvotes;
1067 this.setState(this.state);
1068 } else if (res.op == UserOperation.BanUser) {
1069 let data = res.data as BanUserResponse;
1071 .filter(c => c.creator_id == data.user.id)
1072 .forEach(c => (c.banned = data.banned));
1074 .filter(c => c.creator_id == data.user.id)
1075 .forEach(c => (c.banned = data.banned));
1076 this.setState(this.state);
1077 } else if (res.op == UserOperation.AddAdmin) {
1078 let data = res.data as AddAdminResponse;
1079 this.state.admins = data.admins;
1080 this.setState(this.state);
1081 } else if (res.op == UserOperation.SaveUserSettings) {
1082 let data = res.data as LoginResponse;
1083 this.state = this.emptyState;
1084 this.state.userSettingsLoading = false;
1085 this.setState(this.state);
1086 UserService.Instance.login(data);
1087 } else if (res.op == UserOperation.DeleteAccount) {
1088 this.state.deleteAccountLoading = false;
1089 this.state.deleteAccountShowConfirm = false;
1090 this.setState(this.state);
1091 this.context.router.history.push('/');