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,
38 createPostLikeFindRes,
42 import { PostListing } from './post-listing';
43 import { SortSelect } from './sort-select';
44 import { ListingTypeSelect } from './listing-type-select';
45 import { CommentNodes } from './comment-nodes';
46 import { MomentTime } from './moment-time';
47 import { i18n } from '../i18next';
60 follows: Array<CommunityUser>;
61 moderates: Array<CommunityUser>;
62 comments: Array<Comment>;
65 admins: Array<UserView>;
70 avatarLoading: boolean;
71 userSettingsForm: UserSettingsForm;
72 userSettingsLoading: boolean;
73 deleteAccountLoading: boolean;
74 deleteAccountShowConfirm: boolean;
75 deleteAccountForm: DeleteAccountForm;
78 export class User extends Component<any, UserState> {
79 private subscription: Subscription;
80 private emptyState: UserState = {
86 number_of_posts: null,
88 number_of_comments: null,
93 send_notifications_to_email: null,
103 avatarLoading: false,
104 view: this.getViewFromProps(this.props),
105 sort: this.getSortTypeFromProps(this.props),
106 page: this.getPageFromProps(this.props),
110 default_sort_type: null,
111 default_listing_type: null,
114 send_notifications_to_email: null,
117 userSettingsLoading: null,
118 deleteAccountLoading: null,
119 deleteAccountShowConfirm: false,
125 constructor(props: any, context: any) {
126 super(props, context);
128 this.state = this.emptyState;
129 this.handleSortChange = this.handleSortChange.bind(this);
130 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
133 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
137 this.state.user_id = Number(this.props.match.params.id);
138 this.state.username = this.props.match.params.username;
140 this.subscription = WebSocketService.Instance.subject
141 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
143 msg => this.parseMessage(msg),
144 err => console.error(err),
145 () => console.log('complete')
151 get isCurrentUser() {
153 UserService.Instance.user &&
154 UserService.Instance.user.id == this.state.user.id
158 getViewFromProps(props: any): View {
159 return props.match.params.view
160 ? View[capitalizeFirstLetter(props.match.params.view)]
164 getSortTypeFromProps(props: any): SortType {
165 return props.match.params.sort
166 ? routeSortTypeToEnum(props.match.params.sort)
170 getPageFromProps(props: any): number {
171 return props.match.params.page ? Number(props.match.params.page) : 1;
174 componentWillUnmount() {
175 this.subscription.unsubscribe();
178 // Necessary for back button for some reason
179 componentWillReceiveProps(nextProps: any) {
181 nextProps.history.action == 'POP' ||
182 nextProps.history.action == 'PUSH'
184 this.state.view = this.getViewFromProps(nextProps);
185 this.state.sort = this.getSortTypeFromProps(nextProps);
186 this.state.page = this.getPageFromProps(nextProps);
187 this.setState(this.state);
192 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
193 // Necessary if you are on a post and you click another post (same route)
195 lastProps.location.pathname.split('/')[2] !==
196 lastProps.history.location.pathname.split('/')[2]
198 // Couldnt get a refresh working. This does for now.
205 <div class="container">
206 {this.state.loading ? (
208 <svg class="icon icon-spinner spin">
209 <use xlinkHref="#icon-spinner"></use>
214 <div class="col-12 col-md-8">
216 {this.state.user.avatar && showAvatars() && (
220 src={this.state.user.avatar}
221 class="rounded-circle mr-2"
224 <span>/u/{this.state.user.name}</span>
227 {this.state.view == View.Overview && this.overview()}
228 {this.state.view == View.Comments && this.comments()}
229 {this.state.view == View.Posts && this.posts()}
230 {this.state.view == View.Saved && this.overview()}
233 <div class="col-12 col-md-4">
235 {this.isCurrentUser && this.userSettings()}
247 <div className="mb-2">
249 value={this.state.view}
250 onChange={linkEvent(this, this.handleViewChange)}
251 class="custom-select custom-select-sm w-auto"
253 <option disabled>{i18n.t('view')}</option>
254 <option value={View.Overview}>{i18n.t('overview')}</option>
255 <option value={View.Comments}>{i18n.t('comments')}</option>
256 <option value={View.Posts}>{i18n.t('posts')}</option>
257 <option value={View.Saved}>{i18n.t('saved')}</option>
261 sort={this.state.sort}
262 onChange={this.handleSortChange}
267 href={`/feeds/u/${this.state.username}.xml?sort=${
268 SortType[this.state.sort]
273 <svg class="icon mx-2 text-muted small">
274 <use xlinkHref="#icon-rss">#</use>
282 let combined: Array<{ type_: string; data: Comment | Post }> = [];
283 let comments = this.state.comments.map(e => {
284 return { type_: 'comments', data: e };
286 let posts = this.state.posts.map(e => {
287 return { type_: 'posts', data: e };
290 combined.push(...comments);
291 combined.push(...posts);
294 if (this.state.sort == SortType.New) {
295 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
297 combined.sort((a, b) => b.data.score - a.data.score);
304 {i.type_ == 'posts' ? (
306 post={i.data as Post}
307 admins={this.state.admins}
312 nodes={[{ comment: i.data as Comment }]}
313 admins={this.state.admins}
327 nodes={commentsToFlatNodes(this.state.comments)}
328 admins={this.state.admins}
338 {this.state.posts.map(post => (
339 <PostListing post={post} admins={this.state.admins} showCommunity />
346 let user = this.state.user;
349 <div class="card border-secondary mb-3">
350 <div class="card-body">
352 <ul class="list-inline mb-0">
353 <li className="list-inline-item">{user.name}</li>
355 <li className="list-inline-item badge badge-danger">
362 {i18n.t('joined')} <MomentTime data={user} showAgo />
364 <div class="table-responsive mt-1">
365 <table class="table table-bordered table-sm mt-2 mb-0">
367 <td class="text-center" colSpan={2}>
368 {i18n.t('number_of_points', {
369 count: user.post_score + user.comment_score,
375 {i18n.t('number_of_points', { count: user.post_score })}
378 {i18n.t('number_of_posts', { count: user.number_of_posts })}
383 {i18n.t('number_of_points', { count: user.comment_score })}
386 {i18n.t('number_of_comments', {
387 count: user.number_of_comments,
393 {this.isCurrentUser ? (
395 class="btn btn-block btn-secondary mt-3"
396 onClick={linkEvent(this, this.handleLogoutClick)}
403 className={`btn btn-block btn-secondary mt-3 ${!this.state
404 .user.matrix_user_id && 'disabled'}`}
406 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
408 {i18n.t('send_secure_message')}
411 class="btn btn-block btn-secondary mt-3"
412 to={`/create_private_message?recipient_id=${this.state.user.id}`}
414 {i18n.t('send_message')}
427 <div class="card border-secondary mb-3">
428 <div class="card-body">
429 <h5>{i18n.t('settings')}</h5>
430 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
431 <div class="form-group">
432 <label>{i18n.t('avatar')}</label>
433 <form class="d-inline">
435 htmlFor="file-upload"
436 class="pointer ml-4 text-muted small font-weight-bold"
438 {!this.state.userSettingsForm.avatar ? (
439 <span class="btn btn-sm btn-secondary">
440 {i18n.t('upload_avatar')}
446 src={this.state.userSettingsForm.avatar}
447 class="rounded-circle"
454 accept="image/*,video/*"
457 disabled={!UserService.Instance.user}
458 onChange={linkEvent(this, this.handleImageUpload)}
462 <div class="form-group">
463 <label>{i18n.t('language')}</label>
465 value={this.state.userSettingsForm.lang}
466 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
467 class="ml-2 custom-select custom-select-sm w-auto"
469 <option disabled>{i18n.t('language')}</option>
470 <option value="browser">{i18n.t('browser_default')}</option>
471 <option disabled>──</option>
472 {languages.map(lang => (
473 <option value={lang.code}>{lang.name}</option>
477 <div class="form-group">
478 <label>{i18n.t('theme')}</label>
480 value={this.state.userSettingsForm.theme}
481 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
482 class="ml-2 custom-select custom-select-sm w-auto"
484 <option disabled>{i18n.t('theme')}</option>
485 {themes.map(theme => (
486 <option value={theme}>{theme}</option>
490 <form className="form-group">
492 <div class="mr-2">{i18n.t('sort_type')}</div>
495 type_={this.state.userSettingsForm.default_listing_type}
496 onChange={this.handleUserSettingsListingTypeChange}
499 <form className="form-group">
501 <div class="mr-2">{i18n.t('type')}</div>
504 sort={this.state.userSettingsForm.default_sort_type}
505 onChange={this.handleUserSettingsSortTypeChange}
508 <div class="form-group row">
509 <label class="col-lg-3 col-form-label" htmlFor="user-email">
512 <div class="col-lg-9">
517 placeholder={i18n.t('optional')}
518 value={this.state.userSettingsForm.email}
521 this.handleUserSettingsEmailChange
527 <div class="form-group row">
528 <label class="col-lg-5 col-form-label">
529 <a href="https://about.riot.im/" target="_blank">
530 {i18n.t('matrix_user_id')}
533 <div class="col-lg-7">
537 placeholder="@user:example.com"
538 value={this.state.userSettingsForm.matrix_user_id}
541 this.handleUserSettingsMatrixUserIdChange
547 <div class="form-group row">
548 <label class="col-lg-5 col-form-label" htmlFor="user-password">
549 {i18n.t('new_password')}
551 <div class="col-lg-7">
556 value={this.state.userSettingsForm.new_password}
559 this.handleUserSettingsNewPasswordChange
564 <div class="form-group row">
566 class="col-lg-5 col-form-label"
567 htmlFor="user-verify-password"
569 {i18n.t('verify_password')}
571 <div class="col-lg-7">
574 id="user-verify-password"
576 value={this.state.userSettingsForm.new_password_verify}
579 this.handleUserSettingsNewPasswordVerifyChange
584 <div class="form-group row">
586 class="col-lg-5 col-form-label"
587 htmlFor="user-old-password"
589 {i18n.t('old_password')}
591 <div class="col-lg-7">
594 id="user-old-password"
596 value={this.state.userSettingsForm.old_password}
599 this.handleUserSettingsOldPasswordChange
604 {WebSocketService.Instance.site.enable_nsfw && (
605 <div class="form-group">
606 <div class="form-check">
608 class="form-check-input"
611 checked={this.state.userSettingsForm.show_nsfw}
614 this.handleUserSettingsShowNsfwChange
617 <label class="form-check-label" htmlFor="user-show-nsfw">
618 {i18n.t('show_nsfw')}
623 <div class="form-group">
624 <div class="form-check">
626 class="form-check-input"
627 id="user-show-avatars"
629 checked={this.state.userSettingsForm.show_avatars}
632 this.handleUserSettingsShowAvatarsChange
635 <label class="form-check-label" htmlFor="user-show-avatars">
636 {i18n.t('show_avatars')}
640 <div class="form-group">
641 <div class="form-check">
643 class="form-check-input"
644 id="user-send-notifications-to-email"
646 disabled={!this.state.user.email}
648 this.state.userSettingsForm.send_notifications_to_email
652 this.handleUserSettingsSendNotificationsToEmailChange
656 class="form-check-label"
657 htmlFor="user-send-notifications-to-email"
659 {i18n.t('send_notifications_to_email')}
663 <div class="form-group">
664 <button type="submit" class="btn btn-block btn-secondary mr-4">
665 {this.state.userSettingsLoading ? (
666 <svg class="icon icon-spinner spin">
667 <use xlinkHref="#icon-spinner"></use>
670 capitalizeFirstLetter(i18n.t('save'))
675 <div class="form-group mb-0">
677 class="btn btn-block btn-danger"
680 this.handleDeleteAccountShowConfirmToggle
683 {i18n.t('delete_account')}
685 {this.state.deleteAccountShowConfirm && (
687 <div class="my-2 alert alert-danger" role="alert">
688 {i18n.t('delete_account_confirm')}
692 value={this.state.deleteAccountForm.password}
695 this.handleDeleteAccountPasswordChange
697 class="form-control my-2"
700 class="btn btn-danger mr-4"
701 disabled={!this.state.deleteAccountForm.password}
702 onClick={linkEvent(this, this.handleDeleteAccount)}
704 {this.state.deleteAccountLoading ? (
705 <svg class="icon icon-spinner spin">
706 <use xlinkHref="#icon-spinner"></use>
709 capitalizeFirstLetter(i18n.t('delete'))
713 class="btn btn-secondary"
716 this.handleDeleteAccountShowConfirmToggle
734 {this.state.moderates.length > 0 && (
735 <div class="card border-secondary mb-3">
736 <div class="card-body">
737 <h5>{i18n.t('moderates')}</h5>
738 <ul class="list-unstyled mb-0">
739 {this.state.moderates.map(community => (
741 <Link to={`/c/${community.community_name}`}>
742 {community.community_name}
757 {this.state.follows.length > 0 && (
758 <div class="card border-secondary mb-3">
759 <div class="card-body">
760 <h5>{i18n.t('subscribed')}</h5>
761 <ul class="list-unstyled mb-0">
762 {this.state.follows.map(community => (
764 <Link to={`/c/${community.community_name}`}>
765 {community.community_name}
780 {this.state.page > 1 && (
782 class="btn btn-sm btn-secondary mr-1"
783 onClick={linkEvent(this, this.prevPage)}
789 class="btn btn-sm btn-secondary"
790 onClick={linkEvent(this, this.nextPage)}
799 let viewStr = View[this.state.view].toLowerCase();
800 let sortStr = SortType[this.state.sort].toLowerCase();
801 this.props.history.push(
802 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
821 let form: GetUserDetailsForm = {
822 user_id: this.state.user_id,
823 username: this.state.username,
824 sort: SortType[this.state.sort],
825 saved_only: this.state.view == View.Saved,
826 page: this.state.page,
829 WebSocketService.Instance.getUserDetails(form);
832 handleSortChange(val: SortType) {
833 this.state.sort = val;
835 this.setState(this.state);
840 handleViewChange(i: User, event: any) {
841 i.state.view = Number(event.target.value);
848 handleUserSettingsShowNsfwChange(i: User, event: any) {
849 i.state.userSettingsForm.show_nsfw = event.target.checked;
853 handleUserSettingsShowAvatarsChange(i: User, event: any) {
854 i.state.userSettingsForm.show_avatars = event.target.checked;
855 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
859 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
860 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
864 handleUserSettingsThemeChange(i: User, event: any) {
865 i.state.userSettingsForm.theme = event.target.value;
866 setTheme(event.target.value);
870 handleUserSettingsLangChange(i: User, event: any) {
871 i.state.userSettingsForm.lang = event.target.value;
872 i18n.changeLanguage(i.state.userSettingsForm.lang);
876 handleUserSettingsSortTypeChange(val: SortType) {
877 this.state.userSettingsForm.default_sort_type = val;
878 this.setState(this.state);
881 handleUserSettingsListingTypeChange(val: ListingType) {
882 this.state.userSettingsForm.default_listing_type = val;
883 this.setState(this.state);
886 handleUserSettingsEmailChange(i: User, event: any) {
887 i.state.userSettingsForm.email = event.target.value;
888 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
889 i.state.userSettingsForm.email = undefined;
894 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
895 i.state.userSettingsForm.matrix_user_id = event.target.value;
897 i.state.userSettingsForm.matrix_user_id == '' &&
898 !i.state.user.matrix_user_id
900 i.state.userSettingsForm.matrix_user_id = undefined;
905 handleUserSettingsNewPasswordChange(i: User, event: any) {
906 i.state.userSettingsForm.new_password = event.target.value;
907 if (i.state.userSettingsForm.new_password == '') {
908 i.state.userSettingsForm.new_password = undefined;
913 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
914 i.state.userSettingsForm.new_password_verify = event.target.value;
915 if (i.state.userSettingsForm.new_password_verify == '') {
916 i.state.userSettingsForm.new_password_verify = undefined;
921 handleUserSettingsOldPasswordChange(i: User, event: any) {
922 i.state.userSettingsForm.old_password = event.target.value;
923 if (i.state.userSettingsForm.old_password == '') {
924 i.state.userSettingsForm.old_password = undefined;
929 handleImageUpload(i: User, event: any) {
930 event.preventDefault();
931 let file = event.target.files[0];
932 const imageUploadUrl = `/pictshare/api/upload.php`;
933 const formData = new FormData();
934 formData.append('file', file);
936 i.state.avatarLoading = true;
939 fetch(imageUploadUrl, {
943 .then(res => res.json())
945 let url = `${window.location.origin}/pictshare/${res.url}`;
946 if (res.filetype == 'mp4') {
949 i.state.userSettingsForm.avatar = url;
951 i.state.avatarLoading = false;
955 i.state.avatarLoading = false;
957 toast(error, 'danger');
961 handleUserSettingsSubmit(i: User, event: any) {
962 event.preventDefault();
963 i.state.userSettingsLoading = true;
966 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
969 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
970 event.preventDefault();
971 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
975 handleDeleteAccountPasswordChange(i: User, event: any) {
976 i.state.deleteAccountForm.password = event.target.value;
980 handleLogoutClick(i: User) {
981 UserService.Instance.logout();
982 i.context.router.history.push('/');
985 handleDeleteAccount(i: User, event: any) {
986 event.preventDefault();
987 i.state.deleteAccountLoading = true;
990 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
993 parseMessage(msg: WebSocketJsonResponse) {
995 let res = wsJsonToRes(msg);
997 toast(i18n.t(msg.error), 'danger');
998 this.state.deleteAccountLoading = false;
999 this.state.avatarLoading = false;
1000 this.state.userSettingsLoading = false;
1001 if (msg.error == 'couldnt_find_that_username_or_email') {
1002 this.context.router.history.push('/');
1004 this.setState(this.state);
1006 } else if (msg.reconnect) {
1008 } else if (res.op == UserOperation.GetUserDetails) {
1009 let data = res.data as UserDetailsResponse;
1010 this.state.user = data.user;
1011 this.state.comments = data.comments;
1012 this.state.follows = data.follows;
1013 this.state.moderates = data.moderates;
1014 this.state.posts = data.posts;
1015 this.state.admins = data.admins;
1016 this.state.loading = false;
1017 if (this.isCurrentUser) {
1018 this.state.userSettingsForm.show_nsfw =
1019 UserService.Instance.user.show_nsfw;
1020 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1021 ? UserService.Instance.user.theme
1023 this.state.userSettingsForm.default_sort_type =
1024 UserService.Instance.user.default_sort_type;
1025 this.state.userSettingsForm.default_listing_type =
1026 UserService.Instance.user.default_listing_type;
1027 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1028 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1029 this.state.userSettingsForm.email = this.state.user.email;
1030 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1031 this.state.userSettingsForm.show_avatars =
1032 UserService.Instance.user.show_avatars;
1033 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1035 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
1036 window.scrollTo(0, 0);
1037 this.setState(this.state);
1039 } else if (res.op == UserOperation.EditComment) {
1040 let data = res.data as CommentResponse;
1041 editCommentRes(data, this.state.comments);
1042 this.setState(this.state);
1043 } else if (res.op == UserOperation.CreateComment) {
1044 let data = res.data as CommentResponse;
1046 UserService.Instance.user &&
1047 data.comment.creator_id == UserService.Instance.user.id
1049 toast(i18n.t('reply_sent'));
1051 } else if (res.op == UserOperation.SaveComment) {
1052 let data = res.data as CommentResponse;
1053 saveCommentRes(data, this.state.comments);
1054 this.setState(this.state);
1055 } else if (res.op == UserOperation.CreateCommentLike) {
1056 let data = res.data as CommentResponse;
1057 createCommentLikeRes(data, this.state.comments);
1058 this.setState(this.state);
1059 } else if (res.op == UserOperation.CreatePostLike) {
1060 let data = res.data as PostResponse;
1061 createPostLikeFindRes(data, this.state.posts);
1062 this.setState(this.state);
1063 } else if (res.op == UserOperation.BanUser) {
1064 let data = res.data as BanUserResponse;
1066 .filter(c => c.creator_id == data.user.id)
1067 .forEach(c => (c.banned = data.banned));
1069 .filter(c => c.creator_id == data.user.id)
1070 .forEach(c => (c.banned = data.banned));
1071 this.setState(this.state);
1072 } else if (res.op == UserOperation.AddAdmin) {
1073 let data = res.data as AddAdminResponse;
1074 this.state.admins = data.admins;
1075 this.setState(this.state);
1076 } else if (res.op == UserOperation.SaveUserSettings) {
1077 let data = res.data as LoginResponse;
1078 this.state = this.emptyState;
1079 this.state.userSettingsLoading = false;
1080 this.setState(this.state);
1081 UserService.Instance.login(data);
1082 } else if (res.op == UserOperation.DeleteAccount) {
1083 this.state.deleteAccountLoading = false;
1084 this.state.deleteAccountShowConfirm = false;
1085 this.setState(this.state);
1086 this.context.router.history.push('/');