1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Link } from 'inferno-router';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
15 WebSocketJsonResponse,
20 } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
26 capitalizeFirstLetter,
34 import { UserListing } from './user-listing';
35 import { SortSelect } from './sort-select';
36 import { ListingTypeSelect } from './listing-type-select';
37 import { MomentTime } from './moment-time';
38 import { i18n } from '../i18next';
39 import moment from 'moment';
40 import { UserDetails } from './user-details';
46 follows: Array<CommunityUser>;
47 moderates: Array<CommunityUser>;
48 view: UserDetailsView;
52 avatarLoading: boolean;
53 userSettingsForm: UserSettingsForm;
54 userSettingsLoading: boolean;
55 deleteAccountLoading: boolean;
56 deleteAccountShowConfirm: boolean;
57 deleteAccountForm: DeleteAccountForm;
58 siteRes: GetSiteResponse;
62 view: UserDetailsView;
65 user_id: number | null;
75 export class User extends Component<any, UserState> {
76 private subscription: Subscription;
77 private emptyState: UserState = {
82 number_of_posts: null,
84 number_of_comments: null,
89 send_notifications_to_email: null,
99 view: User.getViewFromProps(this.props.match.view),
100 sort: User.getSortTypeFromProps(this.props.match.sort),
101 page: User.getPageFromProps(this.props.match.page),
105 default_sort_type: null,
106 default_listing_type: null,
109 send_notifications_to_email: null,
112 userSettingsLoading: null,
113 deleteAccountLoading: null,
114 deleteAccountShowConfirm: false,
125 creator_id: undefined,
126 published: undefined,
127 creator_name: undefined,
128 number_of_users: undefined,
129 number_of_posts: undefined,
130 number_of_comments: undefined,
131 number_of_communities: undefined,
132 enable_downvotes: undefined,
133 open_registration: undefined,
134 enable_nsfw: undefined,
140 constructor(props: any, context: any) {
141 super(props, context);
143 this.state = this.emptyState;
144 this.handleSortChange = this.handleSortChange.bind(this);
145 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
148 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
151 this.handlePageChange = this.handlePageChange.bind(this);
153 this.state.user_id = Number(this.props.match.params.id) || null;
154 this.state.username = this.props.match.params.username;
156 this.subscription = WebSocketService.Instance.subject
157 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
159 msg => this.parseMessage(msg),
160 err => console.error(err),
161 () => console.log('complete')
164 WebSocketService.Instance.getSite();
167 get isCurrentUser() {
169 UserService.Instance.user &&
170 UserService.Instance.user.id == this.state.user.id
174 static getViewFromProps(view: any): UserDetailsView {
176 ? UserDetailsView[capitalizeFirstLetter(view)]
177 : UserDetailsView.Overview;
180 static getSortTypeFromProps(sort: any): SortType {
181 return sort ? routeSortTypeToEnum(sort) : SortType.New;
184 static getPageFromProps(page: any): number {
185 return page ? Number(page) : 1;
188 componentWillUnmount() {
189 this.subscription.unsubscribe();
192 static getDerivedStateFromProps(props: any): UserProps {
194 view: this.getViewFromProps(props.match.params.view),
195 sort: this.getSortTypeFromProps(props.match.params.sort),
196 page: this.getPageFromProps(props.match.params.page),
197 user_id: Number(props.match.params.id) || null,
198 username: props.match.params.username,
202 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
203 // Necessary if you are on a post and you click another post (same route)
205 lastProps.location.pathname.split('/')[2] !==
206 lastProps.history.location.pathname.split('/')[2]
208 // Couldnt get a refresh working. This does for now.
214 get documentTitle(): string {
215 if (this.state.siteRes.site.name) {
216 return `@${this.state.username} - ${this.state.siteRes.site.name}`;
224 <div class="container">
225 <Helmet title={this.documentTitle} />
227 <div class="col-12 col-md-8">
229 {this.state.user.avatar && showAvatars() && (
233 src={this.state.user.avatar}
234 class="rounded-circle mr-2"
237 <span>@{this.state.username}</span>
239 {this.state.loading ? (
241 <svg class="icon icon-spinner spin">
242 <use xlinkHref="#icon-spinner"></use>
249 user_id={this.state.user_id}
250 username={this.state.username}
251 sort={SortType[this.state.sort]}
252 page={this.state.page}
254 enableDownvotes={this.state.siteRes.site.enable_downvotes}
255 enableNsfw={this.state.siteRes.site.enable_nsfw}
256 admins={this.state.siteRes.admins}
257 view={this.state.view}
258 onPageChange={this.handlePageChange}
262 {!this.state.loading && (
263 <div class="col-12 col-md-4">
265 {this.isCurrentUser && this.userSettings()}
277 <div class="btn-group btn-group-toggle">
279 className={`btn btn-outline-secondary pointer
280 ${this.state.view == UserDetailsView.Overview && 'active'}
285 value={UserDetailsView.Overview}
286 checked={this.state.view === UserDetailsView.Overview}
287 onChange={linkEvent(this, this.handleViewChange)}
292 className={`btn btn-outline-secondary pointer
293 ${this.state.view == UserDetailsView.Comments && 'active'}
298 value={UserDetailsView.Comments}
299 checked={this.state.view == UserDetailsView.Comments}
300 onChange={linkEvent(this, this.handleViewChange)}
305 className={`btn btn-outline-secondary pointer
306 ${this.state.view == UserDetailsView.Posts && 'active'}
311 value={UserDetailsView.Posts}
312 checked={this.state.view == UserDetailsView.Posts}
313 onChange={linkEvent(this, this.handleViewChange)}
318 className={`btn btn-outline-secondary pointer
319 ${this.state.view == UserDetailsView.Saved && 'active'}
324 value={UserDetailsView.Saved}
325 checked={this.state.view == UserDetailsView.Saved}
326 onChange={linkEvent(this, this.handleViewChange)}
336 <div className="mb-2">
337 <span class="mr-3">{this.viewRadios()}</span>
339 sort={this.state.sort}
340 onChange={this.handleSortChange}
344 href={`/feeds/u/${this.state.username}.xml?sort=${
345 SortType[this.state.sort]
351 <svg class="icon mx-2 text-muted small">
352 <use xlinkHref="#icon-rss">#</use>
360 let user = this.state.user;
363 <div class="card bg-transparent border-secondary mb-3">
364 <div class="card-body">
366 <ul class="list-inline mb-0">
367 <li className="list-inline-item">
368 <UserListing user={user} realLink />
371 <li className="list-inline-item badge badge-danger">
377 <div className="d-flex align-items-center mb-2">
379 <use xlinkHref="#icon-cake"></use>
381 <span className="ml-2">
382 {i18n.t('cake_day_title')}{' '}
383 {moment.utc(user.published).local().format('MMM DD, YYYY')}
387 {i18n.t('joined')} <MomentTime data={user} showAgo />
389 <div class="table-responsive mt-1">
390 <table class="table table-bordered table-sm mt-2 mb-0">
393 <td class="text-center" colSpan={2}>
394 {i18n.t('number_of_points', {
395 count: user.post_score + user.comment_score,
403 {i18n.t('number_of_points', { count: user.post_score })}
407 {i18n.t('number_of_posts', { count: user.number_of_posts })}
413 {i18n.t('number_of_points', { count: user.comment_score })}
417 {i18n.t('number_of_comments', {
418 count: user.number_of_comments,
424 {this.isCurrentUser ? (
426 class="btn btn-block btn-secondary mt-3"
427 onClick={linkEvent(this, this.handleLogoutClick)}
434 className={`btn btn-block btn-secondary mt-3 ${
435 !this.state.user.matrix_user_id && 'disabled'
439 href={`https://matrix.to/#/${this.state.user.matrix_user_id}`}
441 {i18n.t('send_secure_message')}
444 class="btn btn-block btn-secondary mt-3"
445 to={`/create_private_message?recipient_id=${this.state.user.id}`}
447 {i18n.t('send_message')}
460 <div class="card bg-transparent border-secondary mb-3">
461 <div class="card-body">
462 <h5>{i18n.t('settings')}</h5>
463 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
464 <div class="form-group">
465 <label>{i18n.t('avatar')}</label>
466 <form class="d-inline">
468 htmlFor="file-upload"
469 class="pointer ml-4 text-muted small font-weight-bold"
471 {!this.checkSettingsAvatar ? (
472 <span class="btn btn-secondary">
473 {i18n.t('upload_avatar')}
479 src={this.state.userSettingsForm.avatar}
480 class="rounded-circle"
487 accept="image/*,video/*"
490 disabled={!UserService.Instance.user}
491 onChange={linkEvent(this, this.handleImageUpload)}
495 {this.checkSettingsAvatar && (
496 <div class="form-group">
498 class="btn btn-secondary btn-block"
499 onClick={linkEvent(this, this.removeAvatar)}
501 {`${capitalizeFirstLetter(i18n.t('remove'))} ${i18n.t(
507 <div class="form-group">
508 <label>{i18n.t('language')}</label>
510 value={this.state.userSettingsForm.lang}
511 onChange={linkEvent(this, this.handleUserSettingsLangChange)}
512 class="ml-2 custom-select w-auto"
514 <option disabled>{i18n.t('language')}</option>
515 <option value="browser">{i18n.t('browser_default')}</option>
516 <option disabled>──</option>
517 {languages.map(lang => (
518 <option value={lang.code}>{lang.name}</option>
522 <div class="form-group">
523 <label>{i18n.t('theme')}</label>
525 value={this.state.userSettingsForm.theme}
526 onChange={linkEvent(this, this.handleUserSettingsThemeChange)}
527 class="ml-2 custom-select w-auto"
529 <option disabled>{i18n.t('theme')}</option>
530 {themes.map(theme => (
531 <option value={theme}>{theme}</option>
535 <form className="form-group">
537 <div class="mr-2">{i18n.t('sort_type')}</div>
540 type_={this.state.userSettingsForm.default_listing_type}
541 onChange={this.handleUserSettingsListingTypeChange}
544 <form className="form-group">
546 <div class="mr-2">{i18n.t('type')}</div>
549 sort={this.state.userSettingsForm.default_sort_type}
550 onChange={this.handleUserSettingsSortTypeChange}
553 <div class="form-group row">
554 <label class="col-lg-3 col-form-label" htmlFor="user-email">
557 <div class="col-lg-9">
562 placeholder={i18n.t('optional')}
563 value={this.state.userSettingsForm.email}
566 this.handleUserSettingsEmailChange
572 <div class="form-group row">
573 <label class="col-lg-5 col-form-label">
575 href="https://about.riot.im/"
579 {i18n.t('matrix_user_id')}
582 <div class="col-lg-7">
586 placeholder="@user:example.com"
587 value={this.state.userSettingsForm.matrix_user_id}
590 this.handleUserSettingsMatrixUserIdChange
596 <div class="form-group row">
597 <label class="col-lg-5 col-form-label" htmlFor="user-password">
598 {i18n.t('new_password')}
600 <div class="col-lg-7">
605 value={this.state.userSettingsForm.new_password}
606 autoComplete="new-password"
609 this.handleUserSettingsNewPasswordChange
614 <div class="form-group row">
616 class="col-lg-5 col-form-label"
617 htmlFor="user-verify-password"
619 {i18n.t('verify_password')}
621 <div class="col-lg-7">
624 id="user-verify-password"
626 value={this.state.userSettingsForm.new_password_verify}
627 autoComplete="new-password"
630 this.handleUserSettingsNewPasswordVerifyChange
635 <div class="form-group row">
637 class="col-lg-5 col-form-label"
638 htmlFor="user-old-password"
640 {i18n.t('old_password')}
642 <div class="col-lg-7">
645 id="user-old-password"
647 value={this.state.userSettingsForm.old_password}
648 autoComplete="new-password"
651 this.handleUserSettingsOldPasswordChange
656 {this.state.siteRes.site.enable_nsfw && (
657 <div class="form-group">
658 <div class="form-check">
660 class="form-check-input"
663 checked={this.state.userSettingsForm.show_nsfw}
666 this.handleUserSettingsShowNsfwChange
669 <label class="form-check-label" htmlFor="user-show-nsfw">
670 {i18n.t('show_nsfw')}
675 <div class="form-group">
676 <div class="form-check">
678 class="form-check-input"
679 id="user-show-avatars"
681 checked={this.state.userSettingsForm.show_avatars}
684 this.handleUserSettingsShowAvatarsChange
687 <label class="form-check-label" htmlFor="user-show-avatars">
688 {i18n.t('show_avatars')}
692 <div class="form-group">
693 <div class="form-check">
695 class="form-check-input"
696 id="user-send-notifications-to-email"
698 disabled={!this.state.user.email}
700 this.state.userSettingsForm.send_notifications_to_email
704 this.handleUserSettingsSendNotificationsToEmailChange
708 class="form-check-label"
709 htmlFor="user-send-notifications-to-email"
711 {i18n.t('send_notifications_to_email')}
715 <div class="form-group">
716 <button type="submit" class="btn btn-block btn-secondary mr-4">
717 {this.state.userSettingsLoading ? (
718 <svg class="icon icon-spinner spin">
719 <use xlinkHref="#icon-spinner"></use>
722 capitalizeFirstLetter(i18n.t('save'))
727 <div class="form-group mb-0">
729 class="btn btn-block btn-danger"
732 this.handleDeleteAccountShowConfirmToggle
735 {i18n.t('delete_account')}
737 {this.state.deleteAccountShowConfirm && (
739 <div class="my-2 alert alert-danger" role="alert">
740 {i18n.t('delete_account_confirm')}
744 value={this.state.deleteAccountForm.password}
745 autoComplete="new-password"
748 this.handleDeleteAccountPasswordChange
750 class="form-control my-2"
753 class="btn btn-danger mr-4"
754 disabled={!this.state.deleteAccountForm.password}
755 onClick={linkEvent(this, this.handleDeleteAccount)}
757 {this.state.deleteAccountLoading ? (
758 <svg class="icon icon-spinner spin">
759 <use xlinkHref="#icon-spinner"></use>
762 capitalizeFirstLetter(i18n.t('delete'))
766 class="btn btn-secondary"
769 this.handleDeleteAccountShowConfirmToggle
787 {this.state.moderates.length > 0 && (
788 <div class="card bg-transparent border-secondary mb-3">
789 <div class="card-body">
790 <h5>{i18n.t('moderates')}</h5>
791 <ul class="list-unstyled mb-0">
792 {this.state.moderates.map(community => (
794 <Link to={`/c/${community.community_name}`}>
795 {community.community_name}
810 {this.state.follows.length > 0 && (
811 <div class="card bg-transparent border-secondary mb-3">
812 <div class="card-body">
813 <h5>{i18n.t('subscribed')}</h5>
814 <ul class="list-unstyled mb-0">
815 {this.state.follows.map(community => (
817 <Link to={`/c/${community.community_name}`}>
818 {community.community_name}
830 updateUrl(paramUpdates: UrlParams) {
831 const page = paramUpdates.page || this.state.page;
833 paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
835 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
836 this.props.history.push(
837 `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
841 handlePageChange(page: number) {
842 this.updateUrl({ page });
845 handleSortChange(val: SortType) {
846 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
849 handleViewChange(i: User, event: any) {
851 view: UserDetailsView[Number(event.target.value)].toLowerCase(),
856 handleUserSettingsShowNsfwChange(i: User, event: any) {
857 i.state.userSettingsForm.show_nsfw = event.target.checked;
861 handleUserSettingsShowAvatarsChange(i: User, event: any) {
862 i.state.userSettingsForm.show_avatars = event.target.checked;
863 UserService.Instance.user.show_avatars = event.target.checked; // Just for instant updates
867 handleUserSettingsSendNotificationsToEmailChange(i: User, event: any) {
868 i.state.userSettingsForm.send_notifications_to_email = event.target.checked;
872 handleUserSettingsThemeChange(i: User, event: any) {
873 i.state.userSettingsForm.theme = event.target.value;
874 setTheme(event.target.value, true);
878 handleUserSettingsLangChange(i: User, event: any) {
879 i.state.userSettingsForm.lang = event.target.value;
880 i18n.changeLanguage(i.state.userSettingsForm.lang);
884 handleUserSettingsSortTypeChange(val: SortType) {
885 this.state.userSettingsForm.default_sort_type = val;
886 this.setState(this.state);
889 handleUserSettingsListingTypeChange(val: ListingType) {
890 this.state.userSettingsForm.default_listing_type = val;
891 this.setState(this.state);
894 handleUserSettingsEmailChange(i: User, event: any) {
895 i.state.userSettingsForm.email = event.target.value;
896 if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
897 i.state.userSettingsForm.email = undefined;
902 handleUserSettingsMatrixUserIdChange(i: User, event: any) {
903 i.state.userSettingsForm.matrix_user_id = event.target.value;
905 i.state.userSettingsForm.matrix_user_id == '' &&
906 !i.state.user.matrix_user_id
908 i.state.userSettingsForm.matrix_user_id = undefined;
913 handleUserSettingsNewPasswordChange(i: User, event: any) {
914 i.state.userSettingsForm.new_password = event.target.value;
915 if (i.state.userSettingsForm.new_password == '') {
916 i.state.userSettingsForm.new_password = undefined;
921 handleUserSettingsNewPasswordVerifyChange(i: User, event: any) {
922 i.state.userSettingsForm.new_password_verify = event.target.value;
923 if (i.state.userSettingsForm.new_password_verify == '') {
924 i.state.userSettingsForm.new_password_verify = undefined;
929 handleUserSettingsOldPasswordChange(i: User, event: any) {
930 i.state.userSettingsForm.old_password = event.target.value;
931 if (i.state.userSettingsForm.old_password == '') {
932 i.state.userSettingsForm.old_password = undefined;
937 handleImageUpload(i: User, event: any) {
938 event.preventDefault();
939 let file = event.target.files[0];
940 const imageUploadUrl = `/pictrs/image`;
941 const formData = new FormData();
942 formData.append('images[]', file);
944 i.state.avatarLoading = true;
947 fetch(imageUploadUrl, {
951 .then(res => res.json())
953 console.log('pictrs upload:');
955 if (res.msg == 'ok') {
956 let hash = res.files[0].file;
957 let url = `${window.location.origin}/pictrs/image/${hash}`;
958 i.state.userSettingsForm.avatar = url;
959 i.state.avatarLoading = false;
962 i.state.avatarLoading = false;
964 toast(JSON.stringify(res), 'danger');
968 i.state.avatarLoading = false;
970 toast(error, 'danger');
974 removeAvatar(i: User, event: any) {
975 event.preventDefault();
976 i.state.userSettingsLoading = true;
977 i.state.userSettingsForm.avatar = '';
980 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
983 get checkSettingsAvatar(): boolean {
985 this.state.userSettingsForm.avatar &&
986 this.state.userSettingsForm.avatar != ''
990 handleUserSettingsSubmit(i: User, event: any) {
991 event.preventDefault();
992 i.state.userSettingsLoading = true;
995 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
998 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
999 event.preventDefault();
1000 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
1001 i.setState(i.state);
1004 handleDeleteAccountPasswordChange(i: User, event: any) {
1005 i.state.deleteAccountForm.password = event.target.value;
1006 i.setState(i.state);
1009 handleLogoutClick(i: User) {
1010 UserService.Instance.logout();
1011 i.context.router.history.push('/');
1014 handleDeleteAccount(i: User, event: any) {
1015 event.preventDefault();
1016 i.state.deleteAccountLoading = true;
1017 i.setState(i.state);
1019 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
1022 parseMessage(msg: WebSocketJsonResponse) {
1024 const res = wsJsonToRes(msg);
1026 toast(i18n.t(msg.error), 'danger');
1027 if (msg.error == 'couldnt_find_that_username_or_email') {
1028 this.context.router.history.push('/');
1031 deleteAccountLoading: false,
1032 avatarLoading: false,
1033 userSettingsLoading: false,
1036 } else if (res.op == UserOperation.GetUserDetails) {
1037 // Since the UserDetails contains posts/comments as well as some general user info we listen here as well
1038 // and set the parent state if it is not set or differs
1039 const data = res.data as UserDetailsResponse;
1041 if (this.state.user.id !== data.user.id) {
1042 this.state.user = data.user;
1043 this.state.follows = data.follows;
1044 this.state.moderates = data.moderates;
1046 if (this.isCurrentUser) {
1047 this.state.userSettingsForm.show_nsfw =
1048 UserService.Instance.user.show_nsfw;
1049 this.state.userSettingsForm.theme = UserService.Instance.user.theme
1050 ? UserService.Instance.user.theme
1052 this.state.userSettingsForm.default_sort_type =
1053 UserService.Instance.user.default_sort_type;
1054 this.state.userSettingsForm.default_listing_type =
1055 UserService.Instance.user.default_listing_type;
1056 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
1057 this.state.userSettingsForm.avatar = UserService.Instance.user.avatar;
1058 this.state.userSettingsForm.email = this.state.user.email;
1059 this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
1060 this.state.userSettingsForm.show_avatars =
1061 UserService.Instance.user.show_avatars;
1062 this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
1064 this.state.loading = false;
1065 this.setState(this.state);
1067 } else if (res.op == UserOperation.SaveUserSettings) {
1068 const data = res.data as LoginResponse;
1069 UserService.Instance.login(data);
1071 userSettingsLoading: false,
1073 window.scrollTo(0, 0);
1074 } else if (res.op == UserOperation.DeleteAccount) {
1076 deleteAccountLoading: false,
1077 deleteAccountShowConfirm: false,
1079 this.context.router.history.push('/');
1080 } else if (res.op == UserOperation.GetSite) {
1081 const data = res.data as GetSiteResponse;
1082 this.state.siteRes = data;
1083 this.setState(this.state);
1084 } else if (res.op == UserOperation.AddAdmin) {
1085 const data = res.data as AddAdminResponse;
1086 this.state.siteRes.admins = data.admins;
1087 this.setState(this.state);