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';
8 GetFollowedCommunitiesResponse,
10 ListCommunitiesResponse,
27 WebSocketJsonResponse,
28 } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { ListingTypeSelect } from './listing-type-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { SiteForm } from './site-form';
41 pictshareAvatarThumbnail,
44 getListingTypeFromProps,
51 createPostLikeFindRes,
56 import { i18n } from '../i18next';
57 import { T } from 'inferno-i18next';
60 subscribedCommunities: Array<CommunityUser>;
61 trendingCommunities: Array<Community>;
62 siteRes: GetSiteResponse;
63 showEditSite: boolean;
66 comments: Array<Comment>;
67 listingType: ListingType;
73 export class Main extends Component<any, MainState> {
74 private subscription: Subscription;
75 private emptyState: MainState = {
76 subscribedCommunities: [],
77 trendingCommunities: [],
85 number_of_users: null,
86 number_of_posts: null,
87 number_of_comments: null,
88 number_of_communities: null,
89 enable_downvotes: null,
90 open_registration: null,
101 listingType: getListingTypeFromProps(this.props),
102 dataType: getDataTypeFromProps(this.props),
103 sort: getSortTypeFromProps(this.props),
104 page: getPageFromProps(this.props),
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.state = this.emptyState;
111 this.handleEditCancel = this.handleEditCancel.bind(this);
112 this.handleSortChange = this.handleSortChange.bind(this);
113 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
114 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
116 this.subscription = WebSocketService.Instance.subject
117 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
119 msg => this.parseMessage(msg),
120 err => console.error(err),
121 () => console.log('complete')
124 WebSocketService.Instance.getSite();
126 if (UserService.Instance.user) {
127 WebSocketService.Instance.getFollowedCommunities();
130 let listCommunitiesForm: ListCommunitiesForm = {
131 sort: SortType[SortType.Hot],
135 WebSocketService.Instance.listCommunities(listCommunitiesForm);
140 componentWillUnmount() {
141 this.subscription.unsubscribe();
144 // Necessary for back button for some reason
145 componentWillReceiveProps(nextProps: any) {
147 nextProps.history.action == 'POP' ||
148 nextProps.history.action == 'PUSH'
150 this.state.listingType = getListingTypeFromProps(nextProps);
151 this.state.dataType = getDataTypeFromProps(nextProps);
152 this.state.sort = getSortTypeFromProps(nextProps);
153 this.state.page = getPageFromProps(nextProps);
154 this.setState(this.state);
161 <div class="container">
163 <main role="main" class="col-12 col-md-8">
166 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
175 {!this.state.loading && (
177 <div class="card border-secondary mb-3">
178 <div class="card-body">
179 {this.trendingCommunities()}
180 {UserService.Instance.user &&
181 this.state.subscribedCommunities.length > 0 && (
184 <T i18nKey="subscribed_to_communities">
186 <Link class="text-white" to="/communities">
191 <ul class="list-inline">
192 {this.state.subscribedCommunities.map(community => (
193 <li class="list-inline-item">
194 <Link to={`/c/${community.community_name}`}>
195 {community.community_name}
203 class="btn btn-sm btn-secondary btn-block"
204 to="/create_community"
206 {i18n.t('create_a_community')}
218 trendingCommunities() {
222 <T i18nKey="trending_communities">
224 <Link class="text-white" to="/communities">
229 <ul class="list-inline">
230 {this.state.trendingCommunities.map(community => (
231 <li class="list-inline-item">
232 <Link to={`/c/${community.name}`}>{community.name}</Link>
243 {!this.state.showEditSite ? (
247 site={this.state.siteRes.site}
248 onCancel={this.handleEditCancel}
256 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
257 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
258 let sortStr = SortType[this.state.sort].toLowerCase();
259 this.props.history.push(
260 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
267 <div class="card border-secondary mb-3">
268 <div class="card-body">
269 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
271 <ul class="list-inline mb-1 text-muted font-weight-bold">
272 <li className="list-inline-item-action">
275 onClick={linkEvent(this, this.handleEditClick)}
276 data-tippy-content={i18n.t('edit')}
278 <svg class="icon icon-inline">
279 <use xlinkHref="#icon-edit"></use>
285 <ul class="my-2 list-inline">
286 <li className="list-inline-item badge badge-secondary">
287 {i18n.t('number_online', { count: this.state.siteRes.online })}
289 <li className="list-inline-item badge badge-secondary">
290 {i18n.t('number_of_users', {
291 count: this.state.siteRes.site.number_of_users,
294 <li className="list-inline-item badge badge-secondary">
295 {i18n.t('number_of_communities', {
296 count: this.state.siteRes.site.number_of_communities,
299 <li className="list-inline-item badge badge-secondary">
300 {i18n.t('number_of_posts', {
301 count: this.state.siteRes.site.number_of_posts,
304 <li className="list-inline-item badge badge-secondary">
305 {i18n.t('number_of_comments', {
306 count: this.state.siteRes.site.number_of_comments,
309 <li className="list-inline-item">
310 <Link className="badge badge-secondary" to="/modlog">
315 <ul class="mt-1 list-inline small mb-0">
316 <li class="list-inline-item">{i18n.t('admins')}:</li>
317 {this.state.siteRes.admins.map(admin => (
318 <li class="list-inline-item">
319 <Link class="text-info" to={`/u/${admin.name}`}>
320 {admin.avatar && showAvatars() && (
324 src={pictshareAvatarThumbnail(admin.avatar)}
325 class="rounded-circle mr-1"
328 <span>{admin.name}</span>
335 {this.state.siteRes.site.description && (
336 <div class="card border-secondary mb-3">
337 <div class="card-body">
340 dangerouslySetInnerHTML={mdToHtml(
341 this.state.siteRes.site.description
353 <div class="card border-secondary">
354 <div class="card-body">
356 {i18n.t('powered_by')}
357 <svg class="icon mx-2">
358 <use xlinkHref="#icon-mouse">#</use>
365 <T i18nKey="landing_0">
367 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
370 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
376 <a href={repoUrl}>#</a>
378 <a href="https://www.rust-lang.org">#</a>
379 <a href="https://actix.rs/">#</a>
380 <a href="https://infernojs.org">#</a>
381 <a href="https://www.typescriptlang.org/">#</a>
391 <div class="main-content-wrapper">
393 {this.state.loading ? (
395 <svg class="icon icon-spinner spin">
396 <use xlinkHref="#icon-spinner"></use>
410 return this.state.dataType == DataType.Post ? (
412 posts={this.state.posts}
415 sort={this.state.sort}
419 nodes={commentsToFlatNodes(this.state.comments)}
422 sortType={this.state.sort}
429 <div className="mb-3">
432 type_={this.state.dataType}
433 onChange={this.handleDataTypeChange}
438 type_={this.state.listingType}
439 onChange={this.handleListingTypeChange}
443 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
445 {this.state.listingType == ListingType.All && (
447 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
451 <svg class="icon text-muted small">
452 <use xlinkHref="#icon-rss">#</use>
456 {UserService.Instance.user &&
457 this.state.listingType == ListingType.Subscribed && (
459 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
460 SortType[this.state.sort]
465 <svg class="icon text-muted small">
466 <use xlinkHref="#icon-rss">#</use>
477 {this.state.page > 1 && (
479 class="btn btn-sm btn-secondary mr-1"
480 onClick={linkEvent(this, this.prevPage)}
485 {this.state.posts.length == fetchLimit && (
487 class="btn btn-sm btn-secondary"
488 onClick={linkEvent(this, this.nextPage)}
497 get canAdmin(): boolean {
499 UserService.Instance.user &&
500 this.state.siteRes.admins
502 .includes(UserService.Instance.user.id)
506 handleEditClick(i: Main) {
507 i.state.showEditSite = true;
512 this.state.showEditSite = false;
513 this.setState(this.state);
518 i.state.loading = true;
522 window.scrollTo(0, 0);
527 i.state.loading = true;
531 window.scrollTo(0, 0);
534 handleSortChange(val: SortType) {
535 this.state.sort = val;
537 this.state.loading = true;
538 this.setState(this.state);
541 window.scrollTo(0, 0);
544 handleListingTypeChange(val: ListingType) {
545 this.state.listingType = val;
547 this.state.loading = true;
548 this.setState(this.state);
551 window.scrollTo(0, 0);
554 handleDataTypeChange(val: DataType) {
555 this.state.dataType = val;
557 this.state.loading = true;
558 this.setState(this.state);
561 window.scrollTo(0, 0);
565 if (this.state.dataType == DataType.Post) {
566 let getPostsForm: GetPostsForm = {
567 page: this.state.page,
569 sort: SortType[this.state.sort],
570 type_: ListingType[this.state.listingType],
572 WebSocketService.Instance.getPosts(getPostsForm);
574 let getCommentsForm: GetCommentsForm = {
575 page: this.state.page,
577 sort: SortType[this.state.sort],
578 type_: ListingType[this.state.listingType],
580 WebSocketService.Instance.getComments(getCommentsForm);
584 parseMessage(msg: WebSocketJsonResponse) {
586 let res = wsJsonToRes(msg);
588 toast(i18n.t(msg.error), 'danger');
590 } else if (msg.reconnect) {
592 } else if (res.op == UserOperation.GetFollowedCommunities) {
593 let data = res.data as GetFollowedCommunitiesResponse;
594 this.state.subscribedCommunities = data.communities;
595 this.setState(this.state);
596 } else if (res.op == UserOperation.ListCommunities) {
597 let data = res.data as ListCommunitiesResponse;
598 this.state.trendingCommunities = data.communities;
599 this.setState(this.state);
600 } else if (res.op == UserOperation.GetSite) {
601 let data = res.data as GetSiteResponse;
603 // This means it hasn't been set up yet
605 this.context.router.history.push('/setup');
607 this.state.siteRes.admins = data.admins;
608 this.state.siteRes.site = data.site;
609 this.state.siteRes.banned = data.banned;
610 this.state.siteRes.online = data.online;
611 this.setState(this.state);
612 document.title = `${WebSocketService.Instance.site.name}`;
613 } else if (res.op == UserOperation.EditSite) {
614 let data = res.data as SiteResponse;
615 this.state.siteRes.site = data.site;
616 this.state.showEditSite = false;
617 this.setState(this.state);
618 } else if (res.op == UserOperation.GetPosts) {
619 let data = res.data as GetPostsResponse;
620 this.state.posts = data.posts;
621 this.state.loading = false;
622 this.setState(this.state);
623 } else if (res.op == UserOperation.CreatePost) {
624 let data = res.data as PostResponse;
626 // If you're on subscribed, only push it if you're subscribed.
627 if (this.state.listingType == ListingType.Subscribed) {
629 this.state.subscribedCommunities
630 .map(c => c.community_id)
631 .includes(data.post.community_id)
633 this.state.posts.unshift(data.post);
637 let nsfw = data.post.nsfw || data.post.community_nsfw;
639 // Don't push the post if its nsfw, and don't have that setting on
643 UserService.Instance.user &&
644 UserService.Instance.user.show_nsfw)
646 this.state.posts.unshift(data.post);
647 this.setState(this.state);
650 } else if (res.op == UserOperation.EditPost) {
651 let data = res.data as PostResponse;
652 editPostFindRes(data, this.state.posts);
653 this.setState(this.state);
654 } else if (res.op == UserOperation.CreatePostLike) {
655 let data = res.data as PostResponse;
656 createPostLikeFindRes(data, this.state.posts);
657 this.setState(this.state);
658 } else if (res.op == UserOperation.AddAdmin) {
659 let data = res.data as AddAdminResponse;
660 this.state.siteRes.admins = data.admins;
661 this.setState(this.state);
662 } else if (res.op == UserOperation.BanUser) {
663 let data = res.data as BanUserResponse;
664 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
666 // Remove the banned if its found in the list, and the action is an unban
667 if (found && !data.banned) {
668 this.state.siteRes.banned = this.state.siteRes.banned.filter(
669 i => i.id !== data.user.id
672 this.state.siteRes.banned.push(data.user);
676 .filter(p => p.creator_id == data.user.id)
677 .forEach(p => (p.banned = data.banned));
679 this.setState(this.state);
680 } else if (res.op == UserOperation.GetComments) {
681 let data = res.data as GetCommentsResponse;
682 this.state.comments = data.comments;
683 this.state.loading = false;
684 this.setState(this.state);
685 } else if (res.op == UserOperation.EditComment) {
686 let data = res.data as CommentResponse;
687 editCommentRes(data, this.state.comments);
688 this.setState(this.state);
689 } else if (res.op == UserOperation.CreateComment) {
690 let data = res.data as CommentResponse;
692 // Necessary since it might be a user reply
693 if (data.recipient_ids.length == 0) {
694 // If you're on subscribed, only push it if you're subscribed.
695 if (this.state.listingType == ListingType.Subscribed) {
697 this.state.subscribedCommunities
698 .map(c => c.community_id)
699 .includes(data.comment.community_id)
701 this.state.comments.unshift(data.comment);
704 this.state.comments.unshift(data.comment);
706 this.setState(this.state);
708 } else if (res.op == UserOperation.SaveComment) {
709 let data = res.data as CommentResponse;
710 saveCommentRes(data, this.state.comments);
711 this.setState(this.state);
712 } else if (res.op == UserOperation.CreateCommentLike) {
713 let data = res.data as CommentResponse;
714 createCommentLikeRes(data, this.state.comments);
715 this.setState(this.state);