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';
36 import { UserListing } from './user-listing';
43 getListingTypeFromProps,
50 createPostLikeFindRes,
55 import { i18n } from '../i18next';
56 import { T } from 'inferno-i18next';
59 subscribedCommunities: Array<CommunityUser>;
60 trendingCommunities: Array<Community>;
61 siteRes: GetSiteResponse;
62 showEditSite: boolean;
65 comments: Array<Comment>;
66 listingType: ListingType;
72 export class Main extends Component<any, MainState> {
73 private subscription: Subscription;
74 private emptyState: MainState = {
75 subscribedCommunities: [],
76 trendingCommunities: [],
84 number_of_users: null,
85 number_of_posts: null,
86 number_of_comments: null,
87 number_of_communities: null,
88 enable_downvotes: null,
89 open_registration: null,
100 listingType: getListingTypeFromProps(this.props),
101 dataType: getDataTypeFromProps(this.props),
102 sort: getSortTypeFromProps(this.props),
103 page: getPageFromProps(this.props),
106 constructor(props: any, context: any) {
107 super(props, context);
109 this.state = this.emptyState;
110 this.handleEditCancel = this.handleEditCancel.bind(this);
111 this.handleSortChange = this.handleSortChange.bind(this);
112 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
113 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
115 this.subscription = WebSocketService.Instance.subject
116 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
118 msg => this.parseMessage(msg),
119 err => console.error(err),
120 () => console.log('complete')
123 WebSocketService.Instance.getSite();
125 if (UserService.Instance.user) {
126 WebSocketService.Instance.getFollowedCommunities();
129 let listCommunitiesForm: ListCommunitiesForm = {
130 sort: SortType[SortType.Hot],
134 WebSocketService.Instance.listCommunities(listCommunitiesForm);
139 componentWillUnmount() {
140 this.subscription.unsubscribe();
143 // Necessary for back button for some reason
144 componentWillReceiveProps(nextProps: any) {
146 nextProps.history.action == 'POP' ||
147 nextProps.history.action == 'PUSH'
149 this.state.listingType = getListingTypeFromProps(nextProps);
150 this.state.dataType = getDataTypeFromProps(nextProps);
151 this.state.sort = getSortTypeFromProps(nextProps);
152 this.state.page = getPageFromProps(nextProps);
153 this.setState(this.state);
160 <div class="container">
162 <main role="main" class="col-12 col-md-8">
165 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
174 {!this.state.loading && (
176 <div class="card border-secondary mb-3">
177 <div class="card-body">
178 {this.trendingCommunities()}
179 {UserService.Instance.user &&
180 this.state.subscribedCommunities.length > 0 && (
183 <T i18nKey="subscribed_to_communities">
185 <Link class="text-body" to="/communities">
190 <ul class="list-inline">
191 {this.state.subscribedCommunities.map(community => (
192 <li class="list-inline-item">
193 <Link to={`/c/${community.community_name}`}>
194 {community.community_name}
202 class="btn btn-sm btn-secondary btn-block"
203 to="/create_community"
205 {i18n.t('create_a_community')}
217 trendingCommunities() {
221 <T i18nKey="trending_communities">
223 <Link class="text-body" to="/communities">
228 <ul class="list-inline">
229 {this.state.trendingCommunities.map(community => (
230 <li class="list-inline-item">
231 <Link to={`/c/${community.name}`}>{community.name}</Link>
242 {!this.state.showEditSite ? (
246 site={this.state.siteRes.site}
247 onCancel={this.handleEditCancel}
255 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
256 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
257 let sortStr = SortType[this.state.sort].toLowerCase();
258 this.props.history.push(
259 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
266 <div class="card border-secondary mb-3">
267 <div class="card-body">
268 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
270 <ul class="list-inline mb-1 text-muted font-weight-bold">
271 <li className="list-inline-item-action">
274 onClick={linkEvent(this, this.handleEditClick)}
275 data-tippy-content={i18n.t('edit')}
277 <svg class="icon icon-inline">
278 <use xlinkHref="#icon-edit"></use>
284 <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 })}
290 <li className="list-inline-item badge badge-secondary">
291 {i18n.t('number_of_users', {
292 count: this.state.siteRes.site.number_of_users,
295 <li className="list-inline-item badge badge-secondary">
296 {i18n.t('number_of_communities', {
297 count: this.state.siteRes.site.number_of_communities,
300 <li className="list-inline-item badge badge-secondary">
301 {i18n.t('number_of_posts', {
302 count: this.state.siteRes.site.number_of_posts,
305 <li className="list-inline-item badge badge-secondary">
306 {i18n.t('number_of_comments', {
307 count: this.state.siteRes.site.number_of_comments,
310 <li className="list-inline-item">
311 <Link className="badge badge-secondary" to="/modlog">
316 <ul class="mt-1 list-inline small mb-0">
317 <li class="list-inline-item">{i18n.t('admins')}:</li>
318 {this.state.siteRes.admins.map(admin => (
319 <li class="list-inline-item">
323 avatar: admin.avatar,
331 {this.state.siteRes.site.description && (
332 <div class="card border-secondary mb-3">
333 <div class="card-body">
336 dangerouslySetInnerHTML={mdToHtml(
337 this.state.siteRes.site.description
349 <div class="card border-secondary">
350 <div class="card-body">
352 {i18n.t('powered_by')}
353 <svg class="icon mx-2">
354 <use xlinkHref="#icon-mouse">#</use>
361 <T i18nKey="landing_0">
363 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
366 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
372 <a href={repoUrl}>#</a>
374 <a href="https://www.rust-lang.org">#</a>
375 <a href="https://actix.rs/">#</a>
376 <a href="https://infernojs.org">#</a>
377 <a href="https://www.typescriptlang.org/">#</a>
387 <div class="main-content-wrapper">
389 {this.state.loading ? (
391 <svg class="icon icon-spinner spin">
392 <use xlinkHref="#icon-spinner"></use>
406 return this.state.dataType == DataType.Post ? (
408 posts={this.state.posts}
411 sort={this.state.sort}
415 nodes={commentsToFlatNodes(this.state.comments)}
418 sortType={this.state.sort}
426 <div className="mb-3">
429 type_={this.state.dataType}
430 onChange={this.handleDataTypeChange}
435 type_={this.state.listingType}
436 onChange={this.handleListingTypeChange}
440 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
442 {this.state.listingType == ListingType.All && (
444 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
448 <svg class="icon text-muted small">
449 <use xlinkHref="#icon-rss">#</use>
453 {UserService.Instance.user &&
454 this.state.listingType == ListingType.Subscribed && (
456 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
457 SortType[this.state.sort]
462 <svg class="icon text-muted small">
463 <use xlinkHref="#icon-rss">#</use>
474 {this.state.page > 1 && (
476 class="btn btn-sm btn-secondary mr-1"
477 onClick={linkEvent(this, this.prevPage)}
482 {this.state.posts.length == fetchLimit && (
484 class="btn btn-sm btn-secondary"
485 onClick={linkEvent(this, this.nextPage)}
494 get canAdmin(): boolean {
496 UserService.Instance.user &&
497 this.state.siteRes.admins
499 .includes(UserService.Instance.user.id)
503 handleEditClick(i: Main) {
504 i.state.showEditSite = true;
509 this.state.showEditSite = false;
510 this.setState(this.state);
515 i.state.loading = true;
519 window.scrollTo(0, 0);
524 i.state.loading = true;
528 window.scrollTo(0, 0);
531 handleSortChange(val: SortType) {
532 this.state.sort = val;
534 this.state.loading = true;
535 this.setState(this.state);
538 window.scrollTo(0, 0);
541 handleListingTypeChange(val: ListingType) {
542 this.state.listingType = val;
544 this.state.loading = true;
545 this.setState(this.state);
548 window.scrollTo(0, 0);
551 handleDataTypeChange(val: DataType) {
552 this.state.dataType = val;
554 this.state.loading = true;
555 this.setState(this.state);
558 window.scrollTo(0, 0);
562 if (this.state.dataType == DataType.Post) {
563 let getPostsForm: GetPostsForm = {
564 page: this.state.page,
566 sort: SortType[this.state.sort],
567 type_: ListingType[this.state.listingType],
569 WebSocketService.Instance.getPosts(getPostsForm);
571 let getCommentsForm: GetCommentsForm = {
572 page: this.state.page,
574 sort: SortType[this.state.sort],
575 type_: ListingType[this.state.listingType],
577 WebSocketService.Instance.getComments(getCommentsForm);
581 parseMessage(msg: WebSocketJsonResponse) {
583 let res = wsJsonToRes(msg);
585 toast(i18n.t(msg.error), 'danger');
587 } else if (msg.reconnect) {
589 } else if (res.op == UserOperation.GetFollowedCommunities) {
590 let data = res.data as GetFollowedCommunitiesResponse;
591 this.state.subscribedCommunities = data.communities;
592 this.setState(this.state);
593 } else if (res.op == UserOperation.ListCommunities) {
594 let data = res.data as ListCommunitiesResponse;
595 this.state.trendingCommunities = data.communities;
596 this.setState(this.state);
597 } else if (res.op == UserOperation.GetSite) {
598 let data = res.data as GetSiteResponse;
600 // This means it hasn't been set up yet
602 this.context.router.history.push('/setup');
604 this.state.siteRes.admins = data.admins;
605 this.state.siteRes.site = data.site;
606 this.state.siteRes.banned = data.banned;
607 this.state.siteRes.online = data.online;
608 this.setState(this.state);
609 document.title = `${WebSocketService.Instance.site.name}`;
610 } else if (res.op == UserOperation.EditSite) {
611 let data = res.data as SiteResponse;
612 this.state.siteRes.site = data.site;
613 this.state.showEditSite = false;
614 this.setState(this.state);
615 toast(i18n.t('site_saved'));
616 } else if (res.op == UserOperation.GetPosts) {
617 let data = res.data as GetPostsResponse;
618 this.state.posts = data.posts;
619 this.state.loading = false;
620 this.setState(this.state);
622 } else if (res.op == UserOperation.CreatePost) {
623 let data = res.data as PostResponse;
625 // If you're on subscribed, only push it if you're subscribed.
626 if (this.state.listingType == ListingType.Subscribed) {
628 this.state.subscribedCommunities
629 .map(c => c.community_id)
630 .includes(data.post.community_id)
632 this.state.posts.unshift(data.post);
636 let nsfw = data.post.nsfw || data.post.community_nsfw;
638 // Don't push the post if its nsfw, and don't have that setting on
642 UserService.Instance.user &&
643 UserService.Instance.user.show_nsfw)
645 this.state.posts.unshift(data.post);
648 this.setState(this.state);
649 } else if (res.op == UserOperation.EditPost) {
650 let data = res.data as PostResponse;
651 editPostFindRes(data, this.state.posts);
652 this.setState(this.state);
653 } else if (res.op == UserOperation.CreatePostLike) {
654 let data = res.data as PostResponse;
655 createPostLikeFindRes(data, this.state.posts);
656 this.setState(this.state);
657 } else if (res.op == UserOperation.AddAdmin) {
658 let data = res.data as AddAdminResponse;
659 this.state.siteRes.admins = data.admins;
660 this.setState(this.state);
661 } else if (res.op == UserOperation.BanUser) {
662 let data = res.data as BanUserResponse;
663 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
665 // Remove the banned if its found in the list, and the action is an unban
666 if (found && !data.banned) {
667 this.state.siteRes.banned = this.state.siteRes.banned.filter(
668 i => i.id !== data.user.id
671 this.state.siteRes.banned.push(data.user);
675 .filter(p => p.creator_id == data.user.id)
676 .forEach(p => (p.banned = data.banned));
678 this.setState(this.state);
679 } else if (res.op == UserOperation.GetComments) {
680 let data = res.data as GetCommentsResponse;
681 this.state.comments = data.comments;
682 this.state.loading = false;
683 this.setState(this.state);
684 } else if (res.op == UserOperation.EditComment) {
685 let data = res.data as CommentResponse;
686 editCommentRes(data, this.state.comments);
687 this.setState(this.state);
688 } else if (res.op == UserOperation.CreateComment) {
689 let data = res.data as CommentResponse;
691 // Necessary since it might be a user reply
692 if (data.recipient_ids.length == 0) {
693 // If you're on subscribed, only push it if you're subscribed.
694 if (this.state.listingType == ListingType.Subscribed) {
696 this.state.subscribedCommunities
697 .map(c => c.community_id)
698 .includes(data.comment.community_id)
700 this.state.comments.unshift(data.comment);
703 this.state.comments.unshift(data.comment);
705 this.setState(this.state);
707 } else if (res.op == UserOperation.SaveComment) {
708 let data = res.data as CommentResponse;
709 saveCommentRes(data, this.state.comments);
710 this.setState(this.state);
711 } else if (res.op == UserOperation.CreateCommentLike) {
712 let data = res.data as CommentResponse;
713 createCommentLikeRes(data, this.state.comments);
714 this.setState(this.state);