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';
37 import { CommunityLink } from './community-link';
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-body" to="/communities">
191 <ul class="list-inline">
192 {this.state.subscribedCommunities.map(community => (
193 <li class="list-inline-item">
196 name: community.community_name,
197 id: community.community_id,
198 local: community.community_local,
199 actor_id: community.community_actor_id,
208 class="btn btn-sm btn-secondary btn-block"
209 to="/create_community"
211 {i18n.t('create_a_community')}
223 trendingCommunities() {
227 <T i18nKey="trending_communities">
229 <Link class="text-body" to="/communities">
234 <ul class="list-inline">
235 {this.state.trendingCommunities.map(community => (
236 <li class="list-inline-item">
237 <CommunityLink community={community} />
248 {!this.state.showEditSite ? (
252 site={this.state.siteRes.site}
253 onCancel={this.handleEditCancel}
261 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
262 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
263 let sortStr = SortType[this.state.sort].toLowerCase();
264 this.props.history.push(
265 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
272 <div class="card border-secondary mb-3">
273 <div class="card-body">
274 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
276 <ul class="list-inline mb-1 text-muted font-weight-bold">
277 <li className="list-inline-item-action">
280 onClick={linkEvent(this, this.handleEditClick)}
281 data-tippy-content={i18n.t('edit')}
283 <svg class="icon icon-inline">
284 <use xlinkHref="#icon-edit"></use>
290 <ul class="my-2 list-inline">
292 <li className="list-inline-item badge badge-secondary">
293 {i18n.t('number_online', { count: this.state.siteRes.online })}
296 <li className="list-inline-item badge badge-secondary">
297 {i18n.t('number_of_users', {
298 count: this.state.siteRes.site.number_of_users,
301 <li className="list-inline-item badge badge-secondary">
302 {i18n.t('number_of_communities', {
303 count: this.state.siteRes.site.number_of_communities,
306 <li className="list-inline-item badge badge-secondary">
307 {i18n.t('number_of_posts', {
308 count: this.state.siteRes.site.number_of_posts,
311 <li className="list-inline-item badge badge-secondary">
312 {i18n.t('number_of_comments', {
313 count: this.state.siteRes.site.number_of_comments,
316 <li className="list-inline-item">
317 <Link className="badge badge-secondary" to="/modlog">
322 <ul class="mt-1 list-inline small mb-0">
323 <li class="list-inline-item">{i18n.t('admins')}:</li>
324 {this.state.siteRes.admins.map(admin => (
325 <li class="list-inline-item">
329 avatar: admin.avatar,
331 actor_id: admin.actor_id,
340 {this.state.siteRes.site.description && (
341 <div class="card border-secondary mb-3">
342 <div class="card-body">
345 dangerouslySetInnerHTML={mdToHtml(
346 this.state.siteRes.site.description
358 <div class="card border-secondary">
359 <div class="card-body">
361 {i18n.t('powered_by')}
362 <svg class="icon mx-2">
363 <use xlinkHref="#icon-mouse">#</use>
370 <T i18nKey="landing_0">
372 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
375 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
381 <a href={repoUrl}>#</a>
383 <a href="https://www.rust-lang.org">#</a>
384 <a href="https://actix.rs/">#</a>
385 <a href="https://infernojs.org">#</a>
386 <a href="https://www.typescriptlang.org/">#</a>
396 <div class="main-content-wrapper">
398 {this.state.loading ? (
400 <svg class="icon icon-spinner spin">
401 <use xlinkHref="#icon-spinner"></use>
415 return this.state.dataType == DataType.Post ? (
417 posts={this.state.posts}
420 sort={this.state.sort}
421 enableDownvotes={this.state.siteRes.site.enable_downvotes}
422 enableNsfw={this.state.siteRes.site.enable_nsfw}
426 nodes={commentsToFlatNodes(this.state.comments)}
429 sortType={this.state.sort}
431 enableDownvotes={this.state.siteRes.site.enable_downvotes}
438 <div className="mb-3">
441 type_={this.state.dataType}
442 onChange={this.handleDataTypeChange}
447 type_={this.state.listingType}
448 onChange={this.handleListingTypeChange}
452 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
454 {this.state.listingType == ListingType.All && (
456 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
461 <svg class="icon text-muted small">
462 <use xlinkHref="#icon-rss">#</use>
466 {UserService.Instance.user &&
467 this.state.listingType == ListingType.Subscribed && (
469 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
470 SortType[this.state.sort]
476 <svg class="icon text-muted small">
477 <use xlinkHref="#icon-rss">#</use>
488 {this.state.page > 1 && (
490 class="btn btn-sm btn-secondary mr-1"
491 onClick={linkEvent(this, this.prevPage)}
496 {this.state.posts.length == fetchLimit && (
498 class="btn btn-sm btn-secondary"
499 onClick={linkEvent(this, this.nextPage)}
508 get canAdmin(): boolean {
510 UserService.Instance.user &&
511 this.state.siteRes.admins
513 .includes(UserService.Instance.user.id)
517 handleEditClick(i: Main) {
518 i.state.showEditSite = true;
523 this.state.showEditSite = false;
524 this.setState(this.state);
529 i.state.loading = true;
533 window.scrollTo(0, 0);
538 i.state.loading = true;
542 window.scrollTo(0, 0);
545 handleSortChange(val: SortType) {
546 this.state.sort = val;
548 this.state.loading = true;
549 this.setState(this.state);
552 window.scrollTo(0, 0);
555 handleListingTypeChange(val: ListingType) {
556 this.state.listingType = val;
558 this.state.loading = true;
559 this.setState(this.state);
562 window.scrollTo(0, 0);
565 handleDataTypeChange(val: DataType) {
566 this.state.dataType = val;
568 this.state.loading = true;
569 this.setState(this.state);
572 window.scrollTo(0, 0);
576 if (this.state.dataType == DataType.Post) {
577 let getPostsForm: GetPostsForm = {
578 page: this.state.page,
580 sort: SortType[this.state.sort],
581 type_: ListingType[this.state.listingType],
583 WebSocketService.Instance.getPosts(getPostsForm);
585 let getCommentsForm: GetCommentsForm = {
586 page: this.state.page,
588 sort: SortType[this.state.sort],
589 type_: ListingType[this.state.listingType],
591 WebSocketService.Instance.getComments(getCommentsForm);
595 parseMessage(msg: WebSocketJsonResponse) {
597 let res = wsJsonToRes(msg);
599 toast(i18n.t(msg.error), 'danger');
601 } else if (msg.reconnect) {
603 } else if (res.op == UserOperation.GetFollowedCommunities) {
604 let data = res.data as GetFollowedCommunitiesResponse;
605 this.state.subscribedCommunities = data.communities;
606 this.setState(this.state);
607 } else if (res.op == UserOperation.ListCommunities) {
608 let data = res.data as ListCommunitiesResponse;
609 this.state.trendingCommunities = data.communities;
610 this.setState(this.state);
611 } else if (res.op == UserOperation.GetSite) {
612 let data = res.data as GetSiteResponse;
614 // This means it hasn't been set up yet
616 this.context.router.history.push('/setup');
618 this.state.siteRes.admins = data.admins;
619 this.state.siteRes.site = data.site;
620 this.state.siteRes.banned = data.banned;
621 this.state.siteRes.online = data.online;
622 this.setState(this.state);
623 document.title = `${this.state.siteRes.site.name}`;
624 } else if (res.op == UserOperation.EditSite) {
625 let data = res.data as SiteResponse;
626 this.state.siteRes.site = data.site;
627 this.state.showEditSite = false;
628 this.setState(this.state);
629 toast(i18n.t('site_saved'));
630 } else if (res.op == UserOperation.GetPosts) {
631 let data = res.data as GetPostsResponse;
632 this.state.posts = data.posts;
633 this.state.loading = false;
634 this.setState(this.state);
636 } else if (res.op == UserOperation.CreatePost) {
637 let data = res.data as PostResponse;
639 // If you're on subscribed, only push it if you're subscribed.
640 if (this.state.listingType == ListingType.Subscribed) {
642 this.state.subscribedCommunities
643 .map(c => c.community_id)
644 .includes(data.post.community_id)
646 this.state.posts.unshift(data.post);
650 let nsfw = data.post.nsfw || data.post.community_nsfw;
652 // Don't push the post if its nsfw, and don't have that setting on
656 UserService.Instance.user &&
657 UserService.Instance.user.show_nsfw)
659 this.state.posts.unshift(data.post);
662 this.setState(this.state);
663 } else if (res.op == UserOperation.EditPost) {
664 let data = res.data as PostResponse;
665 editPostFindRes(data, this.state.posts);
666 this.setState(this.state);
667 } else if (res.op == UserOperation.CreatePostLike) {
668 let data = res.data as PostResponse;
669 createPostLikeFindRes(data, this.state.posts);
670 this.setState(this.state);
671 } else if (res.op == UserOperation.AddAdmin) {
672 let data = res.data as AddAdminResponse;
673 this.state.siteRes.admins = data.admins;
674 this.setState(this.state);
675 } else if (res.op == UserOperation.BanUser) {
676 let data = res.data as BanUserResponse;
677 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
679 // Remove the banned if its found in the list, and the action is an unban
680 if (found && !data.banned) {
681 this.state.siteRes.banned = this.state.siteRes.banned.filter(
682 i => i.id !== data.user.id
685 this.state.siteRes.banned.push(data.user);
689 .filter(p => p.creator_id == data.user.id)
690 .forEach(p => (p.banned = data.banned));
692 this.setState(this.state);
693 } else if (res.op == UserOperation.GetComments) {
694 let data = res.data as GetCommentsResponse;
695 this.state.comments = data.comments;
696 this.state.loading = false;
697 this.setState(this.state);
698 } else if (res.op == UserOperation.EditComment) {
699 let data = res.data as CommentResponse;
700 editCommentRes(data, this.state.comments);
701 this.setState(this.state);
702 } else if (res.op == UserOperation.CreateComment) {
703 let data = res.data as CommentResponse;
705 // Necessary since it might be a user reply
706 if (data.recipient_ids.length == 0) {
707 // If you're on subscribed, only push it if you're subscribed.
708 if (this.state.listingType == ListingType.Subscribed) {
710 this.state.subscribedCommunities
711 .map(c => c.community_id)
712 .includes(data.comment.community_id)
714 this.state.comments.unshift(data.comment);
717 this.state.comments.unshift(data.comment);
719 this.setState(this.state);
721 } else if (res.op == UserOperation.SaveComment) {
722 let data = res.data as CommentResponse;
723 saveCommentRes(data, this.state.comments);
724 this.setState(this.state);
725 } else if (res.op == UserOperation.CreateCommentLike) {
726 let data = res.data as CommentResponse;
727 createCommentLikeRes(data, this.state.comments);
728 this.setState(this.state);