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 small font-weight-bold">
272 <li className="list-inline-item">
275 onClick={linkEvent(this, this.handleEditClick)}
282 <ul class="my-2 list-inline">
283 <li className="list-inline-item badge badge-secondary">
284 {i18n.t('number_online', { count: this.state.siteRes.online })}
286 <li className="list-inline-item badge badge-secondary">
287 {i18n.t('number_of_users', {
288 count: this.state.siteRes.site.number_of_users,
291 <li className="list-inline-item badge badge-secondary">
292 {i18n.t('number_of_communities', {
293 count: this.state.siteRes.site.number_of_communities,
296 <li className="list-inline-item badge badge-secondary">
297 {i18n.t('number_of_posts', {
298 count: this.state.siteRes.site.number_of_posts,
301 <li className="list-inline-item badge badge-secondary">
302 {i18n.t('number_of_comments', {
303 count: this.state.siteRes.site.number_of_comments,
306 <li className="list-inline-item">
307 <Link className="badge badge-secondary" to="/modlog">
312 <ul class="mt-1 list-inline small mb-0">
313 <li class="list-inline-item">{i18n.t('admins')}:</li>
314 {this.state.siteRes.admins.map(admin => (
315 <li class="list-inline-item">
316 <Link class="text-info" to={`/u/${admin.name}`}>
317 {admin.avatar && showAvatars() && (
321 src={pictshareAvatarThumbnail(admin.avatar)}
322 class="rounded-circle mr-1"
325 <span>{admin.name}</span>
332 {this.state.siteRes.site.description && (
333 <div class="card border-secondary mb-3">
334 <div class="card-body">
337 dangerouslySetInnerHTML={mdToHtml(
338 this.state.siteRes.site.description
350 <div class="card border-secondary">
351 <div class="card-body">
353 {i18n.t('powered_by')}
354 <svg class="icon mx-2">
355 <use xlinkHref="#icon-mouse">#</use>
362 <T i18nKey="landing_0">
364 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
367 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
373 <a href={repoUrl}>#</a>
375 <a href="https://www.rust-lang.org">#</a>
376 <a href="https://actix.rs/">#</a>
377 <a href="https://infernojs.org">#</a>
378 <a href="https://www.typescriptlang.org/">#</a>
388 <div class="main-content-wrapper">
390 {this.state.loading ? (
392 <svg class="icon icon-spinner spin">
393 <use xlinkHref="#icon-spinner"></use>
407 return this.state.dataType == DataType.Post ? (
409 posts={this.state.posts}
412 sort={this.state.sort}
416 nodes={commentsToFlatNodes(this.state.comments)}
419 sortType={this.state.sort}
426 <div className="mb-3">
428 type_={this.state.dataType}
429 onChange={this.handleDataTypeChange}
433 type_={this.state.listingType}
434 onChange={this.handleListingTypeChange}
438 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
440 {this.state.listingType == ListingType.All && (
442 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
446 <svg class="icon mx-1 text-muted small">
447 <use xlinkHref="#icon-rss">#</use>
451 {UserService.Instance.user &&
452 this.state.listingType == ListingType.Subscribed && (
454 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
455 SortType[this.state.sort]
460 <svg class="icon mx-1 text-muted small">
461 <use xlinkHref="#icon-rss">#</use>
472 {this.state.page > 1 && (
474 class="btn btn-sm btn-secondary mr-1"
475 onClick={linkEvent(this, this.prevPage)}
480 {this.state.posts.length == fetchLimit && (
482 class="btn btn-sm btn-secondary"
483 onClick={linkEvent(this, this.nextPage)}
492 get canAdmin(): boolean {
494 UserService.Instance.user &&
495 this.state.siteRes.admins
497 .includes(UserService.Instance.user.id)
501 handleEditClick(i: Main) {
502 i.state.showEditSite = true;
507 this.state.showEditSite = false;
508 this.setState(this.state);
513 i.state.loading = true;
517 window.scrollTo(0, 0);
522 i.state.loading = true;
526 window.scrollTo(0, 0);
529 handleSortChange(val: SortType) {
530 this.state.sort = val;
532 this.state.loading = true;
533 this.setState(this.state);
536 window.scrollTo(0, 0);
539 handleListingTypeChange(val: ListingType) {
540 this.state.listingType = val;
542 this.state.loading = true;
543 this.setState(this.state);
546 window.scrollTo(0, 0);
549 handleDataTypeChange(val: DataType) {
550 this.state.dataType = val;
552 this.state.loading = true;
553 this.setState(this.state);
556 window.scrollTo(0, 0);
560 if (this.state.dataType == DataType.Post) {
561 let getPostsForm: GetPostsForm = {
562 page: this.state.page,
564 sort: SortType[this.state.sort],
565 type_: ListingType[this.state.listingType],
567 WebSocketService.Instance.getPosts(getPostsForm);
569 let getCommentsForm: GetCommentsForm = {
570 page: this.state.page,
572 sort: SortType[this.state.sort],
573 type_: ListingType[this.state.listingType],
575 WebSocketService.Instance.getComments(getCommentsForm);
579 parseMessage(msg: WebSocketJsonResponse) {
581 let res = wsJsonToRes(msg);
583 toast(i18n.t(msg.error), 'danger');
585 } else if (msg.reconnect) {
587 } else if (res.op == UserOperation.GetFollowedCommunities) {
588 let data = res.data as GetFollowedCommunitiesResponse;
589 this.state.subscribedCommunities = data.communities;
590 this.setState(this.state);
591 } else if (res.op == UserOperation.ListCommunities) {
592 let data = res.data as ListCommunitiesResponse;
593 this.state.trendingCommunities = data.communities;
594 this.setState(this.state);
595 } else if (res.op == UserOperation.GetSite) {
596 let data = res.data as GetSiteResponse;
598 // This means it hasn't been set up yet
600 this.context.router.history.push('/setup');
602 this.state.siteRes.admins = data.admins;
603 this.state.siteRes.site = data.site;
604 this.state.siteRes.banned = data.banned;
605 this.state.siteRes.online = data.online;
606 this.setState(this.state);
607 document.title = `${WebSocketService.Instance.site.name}`;
608 } else if (res.op == UserOperation.EditSite) {
609 let data = res.data as SiteResponse;
610 this.state.siteRes.site = data.site;
611 this.state.showEditSite = false;
612 this.setState(this.state);
613 } else if (res.op == UserOperation.GetPosts) {
614 let data = res.data as GetPostsResponse;
615 this.state.posts = data.posts;
616 this.state.loading = false;
617 this.setState(this.state);
618 } else if (res.op == UserOperation.CreatePost) {
619 let data = res.data as PostResponse;
621 // If you're on subscribed, only push it if you're subscribed.
622 if (this.state.listingType == ListingType.Subscribed) {
624 this.state.subscribedCommunities
625 .map(c => c.community_id)
626 .includes(data.post.community_id)
628 this.state.posts.unshift(data.post);
632 let nsfw = data.post.nsfw || data.post.community_nsfw;
634 // Don't push the post if its nsfw, and don't have that setting on
638 UserService.Instance.user &&
639 UserService.Instance.user.show_nsfw)
641 this.state.posts.unshift(data.post);
642 this.setState(this.state);
645 } else if (res.op == UserOperation.EditPost) {
646 let data = res.data as PostResponse;
647 editPostFindRes(data, this.state.posts);
648 this.setState(this.state);
649 } else if (res.op == UserOperation.CreatePostLike) {
650 let data = res.data as PostResponse;
651 createPostLikeFindRes(data, this.state.posts);
652 this.setState(this.state);
653 } else if (res.op == UserOperation.AddAdmin) {
654 let data = res.data as AddAdminResponse;
655 this.state.siteRes.admins = data.admins;
656 this.setState(this.state);
657 } else if (res.op == UserOperation.BanUser) {
658 let data = res.data as BanUserResponse;
659 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
661 // Remove the banned if its found in the list, and the action is an unban
662 if (found && !data.banned) {
663 this.state.siteRes.banned = this.state.siteRes.banned.filter(
664 i => i.id !== data.user.id
667 this.state.siteRes.banned.push(data.user);
671 .filter(p => p.creator_id == data.user.id)
672 .forEach(p => (p.banned = data.banned));
674 this.setState(this.state);
675 } else if (res.op == UserOperation.GetComments) {
676 let data = res.data as GetCommentsResponse;
677 this.state.comments = data.comments;
678 this.state.loading = false;
679 this.setState(this.state);
680 } else if (res.op == UserOperation.EditComment) {
681 let data = res.data as CommentResponse;
682 editCommentRes(data, this.state.comments);
683 this.setState(this.state);
684 } else if (res.op == UserOperation.CreateComment) {
685 let data = res.data as CommentResponse;
687 // Necessary since it might be a user reply
688 if (data.recipient_ids.length == 0) {
689 // If you're on subscribed, only push it if you're subscribed.
690 if (this.state.listingType == ListingType.Subscribed) {
692 this.state.subscribedCommunities
693 .map(c => c.community_id)
694 .includes(data.comment.community_id)
696 this.state.comments.unshift(data.comment);
699 this.state.comments.unshift(data.comment);
701 this.setState(this.state);
703 } else if (res.op == UserOperation.SaveComment) {
704 let data = res.data as CommentResponse;
705 saveCommentRes(data, this.state.comments);
706 this.setState(this.state);
707 } else if (res.op == UserOperation.CreateCommentLike) {
708 let data = res.data as CommentResponse;
709 createCommentLikeRes(data, this.state.comments);
710 this.setState(this.state);