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,
57 import { i18n } from '../i18next';
58 import { T } from 'inferno-i18next';
61 subscribedCommunities: Array<CommunityUser>;
62 trendingCommunities: Array<Community>;
63 siteRes: GetSiteResponse;
64 showEditSite: boolean;
67 comments: Array<Comment>;
68 listingType: ListingType;
74 export class Main extends Component<any, MainState> {
75 private subscription: Subscription;
76 private emptyState: MainState = {
77 subscribedCommunities: [],
78 trendingCommunities: [],
86 number_of_users: null,
87 number_of_posts: null,
88 number_of_comments: null,
89 number_of_communities: null,
90 enable_downvotes: null,
91 open_registration: null,
102 listingType: getListingTypeFromProps(this.props),
103 dataType: getDataTypeFromProps(this.props),
104 sort: getSortTypeFromProps(this.props),
105 page: getPageFromProps(this.props),
108 constructor(props: any, context: any) {
109 super(props, context);
111 this.state = this.emptyState;
112 this.handleEditCancel = this.handleEditCancel.bind(this);
113 this.handleSortChange = this.handleSortChange.bind(this);
114 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
115 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
117 this.subscription = WebSocketService.Instance.subject
118 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
120 msg => this.parseMessage(msg),
121 err => console.error(err),
122 () => console.log('complete')
125 WebSocketService.Instance.getSite();
127 if (UserService.Instance.user) {
128 WebSocketService.Instance.getFollowedCommunities();
131 let listCommunitiesForm: ListCommunitiesForm = {
132 sort: SortType[SortType.Hot],
136 WebSocketService.Instance.listCommunities(listCommunitiesForm);
141 componentWillUnmount() {
142 this.subscription.unsubscribe();
145 // Necessary for back button for some reason
146 componentWillReceiveProps(nextProps: any) {
148 nextProps.history.action == 'POP' ||
149 nextProps.history.action == 'PUSH'
151 this.state.listingType = getListingTypeFromProps(nextProps);
152 this.state.dataType = getDataTypeFromProps(nextProps);
153 this.state.sort = getSortTypeFromProps(nextProps);
154 this.state.page = getPageFromProps(nextProps);
155 this.setState(this.state);
162 <div class="container">
164 <main role="main" class="col-12 col-md-8">
167 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
176 {!this.state.loading && (
178 <div class="card border-secondary mb-3">
179 <div class="card-body">
180 {this.trendingCommunities()}
181 {UserService.Instance.user &&
182 this.state.subscribedCommunities.length > 0 && (
185 <T i18nKey="subscribed_to_communities">
187 <Link class="text-white" to="/communities">
192 <ul class="list-inline">
193 {this.state.subscribedCommunities.map(community => (
194 <li class="list-inline-item">
195 <Link to={`/c/${community.community_name}`}>
196 {community.community_name}
204 class="btn btn-sm btn-secondary btn-block"
205 to="/create_community"
207 {i18n.t('create_a_community')}
219 trendingCommunities() {
223 <T i18nKey="trending_communities">
225 <Link class="text-white" to="/communities">
230 <ul class="list-inline">
231 {this.state.trendingCommunities.map(community => (
232 <li class="list-inline-item">
233 <Link to={`/c/${community.name}`}>{community.name}</Link>
244 {!this.state.showEditSite ? (
248 site={this.state.siteRes.site}
249 onCancel={this.handleEditCancel}
257 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
258 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
259 let sortStr = SortType[this.state.sort].toLowerCase();
260 this.props.history.push(
261 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
268 <div class="card border-secondary mb-3">
269 <div class="card-body">
270 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
272 <ul class="list-inline mb-1 text-muted font-weight-bold">
273 <li className="list-inline-item-action">
276 onClick={linkEvent(this, this.handleEditClick)}
277 data-tippy-content={i18n.t('edit')}
279 <svg class="icon icon-inline">
280 <use xlinkHref="#icon-edit"></use>
286 <ul class="my-2 list-inline">
287 <li className="list-inline-item badge badge-secondary">
288 {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">
320 <Link class="text-info" to={`/u/${admin.name}`}>
321 {admin.avatar && showAvatars() && (
325 src={pictshareAvatarThumbnail(admin.avatar)}
326 class="rounded-circle mr-1"
329 <span>{admin.name}</span>
336 {this.state.siteRes.site.description && (
337 <div class="card border-secondary mb-3">
338 <div class="card-body">
341 dangerouslySetInnerHTML={mdToHtml(
342 this.state.siteRes.site.description
354 <div class="card border-secondary">
355 <div class="card-body">
357 {i18n.t('powered_by')}
358 <svg class="icon mx-2">
359 <use xlinkHref="#icon-mouse">#</use>
366 <T i18nKey="landing_0">
368 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
371 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
377 <a href={repoUrl}>#</a>
379 <a href="https://www.rust-lang.org">#</a>
380 <a href="https://actix.rs/">#</a>
381 <a href="https://infernojs.org">#</a>
382 <a href="https://www.typescriptlang.org/">#</a>
392 <div class="main-content-wrapper">
394 {this.state.loading ? (
396 <svg class="icon icon-spinner spin">
397 <use xlinkHref="#icon-spinner"></use>
411 return this.state.dataType == DataType.Post ? (
413 posts={this.state.posts}
416 sort={this.state.sort}
420 nodes={commentsToFlatNodes(this.state.comments)}
423 sortType={this.state.sort}
430 <div className="mb-3">
433 type_={this.state.dataType}
434 onChange={this.handleDataTypeChange}
439 type_={this.state.listingType}
440 onChange={this.handleListingTypeChange}
444 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
446 {this.state.listingType == ListingType.All && (
448 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
452 <svg class="icon text-muted small">
453 <use xlinkHref="#icon-rss">#</use>
457 {UserService.Instance.user &&
458 this.state.listingType == ListingType.Subscribed && (
460 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
461 SortType[this.state.sort]
466 <svg class="icon text-muted small">
467 <use xlinkHref="#icon-rss">#</use>
478 {this.state.page > 1 && (
480 class="btn btn-sm btn-secondary mr-1"
481 onClick={linkEvent(this, this.prevPage)}
486 {this.state.posts.length == fetchLimit && (
488 class="btn btn-sm btn-secondary"
489 onClick={linkEvent(this, this.nextPage)}
498 get canAdmin(): boolean {
500 UserService.Instance.user &&
501 this.state.siteRes.admins
503 .includes(UserService.Instance.user.id)
507 handleEditClick(i: Main) {
508 i.state.showEditSite = true;
513 this.state.showEditSite = false;
514 this.setState(this.state);
519 i.state.loading = true;
523 window.scrollTo(0, 0);
528 i.state.loading = true;
532 window.scrollTo(0, 0);
535 handleSortChange(val: SortType) {
536 this.state.sort = val;
538 this.state.loading = true;
539 this.setState(this.state);
542 window.scrollTo(0, 0);
545 handleListingTypeChange(val: ListingType) {
546 this.state.listingType = val;
548 this.state.loading = true;
549 this.setState(this.state);
552 window.scrollTo(0, 0);
555 handleDataTypeChange(val: DataType) {
556 this.state.dataType = val;
558 this.state.loading = true;
559 this.setState(this.state);
562 window.scrollTo(0, 0);
566 if (this.state.dataType == DataType.Post) {
567 let getPostsForm: GetPostsForm = {
568 page: this.state.page,
570 sort: SortType[this.state.sort],
571 type_: ListingType[this.state.listingType],
573 WebSocketService.Instance.getPosts(getPostsForm);
575 let getCommentsForm: GetCommentsForm = {
576 page: this.state.page,
578 sort: SortType[this.state.sort],
579 type_: ListingType[this.state.listingType],
581 WebSocketService.Instance.getComments(getCommentsForm);
585 parseMessage(msg: WebSocketJsonResponse) {
587 let res = wsJsonToRes(msg);
589 toast(i18n.t(msg.error), 'danger');
591 } else if (msg.reconnect) {
593 } else if (res.op == UserOperation.GetFollowedCommunities) {
594 let data = res.data as GetFollowedCommunitiesResponse;
595 this.state.subscribedCommunities = data.communities;
596 this.setState(this.state);
597 } else if (res.op == UserOperation.ListCommunities) {
598 let data = res.data as ListCommunitiesResponse;
599 this.state.trendingCommunities = data.communities;
600 this.setState(this.state);
601 } else if (res.op == UserOperation.GetSite) {
602 let data = res.data as GetSiteResponse;
604 // This means it hasn't been set up yet
606 this.context.router.history.push('/setup');
608 this.state.siteRes.admins = data.admins;
609 this.state.siteRes.site = data.site;
610 this.state.siteRes.banned = data.banned;
611 this.state.siteRes.online = data.online;
612 this.setState(this.state);
613 document.title = `${WebSocketService.Instance.site.name}`;
614 } else if (res.op == UserOperation.EditSite) {
615 let data = res.data as SiteResponse;
616 this.state.siteRes.site = data.site;
617 this.state.showEditSite = false;
618 this.setState(this.state);
619 } else if (res.op == UserOperation.GetPosts) {
620 let data = res.data as GetPostsResponse;
621 this.state.posts = data.posts;
622 this.state.loading = false;
623 this.setState(this.state);
625 } else if (res.op == UserOperation.CreatePost) {
626 let data = res.data as PostResponse;
628 // If you're on subscribed, only push it if you're subscribed.
629 if (this.state.listingType == ListingType.Subscribed) {
631 this.state.subscribedCommunities
632 .map(c => c.community_id)
633 .includes(data.post.community_id)
635 this.state.posts.unshift(data.post);
639 let nsfw = data.post.nsfw || data.post.community_nsfw;
641 // Don't push the post if its nsfw, and don't have that setting on
645 UserService.Instance.user &&
646 UserService.Instance.user.show_nsfw)
648 this.state.posts.unshift(data.post);
649 this.setState(this.state);
652 } else if (res.op == UserOperation.EditPost) {
653 let data = res.data as PostResponse;
654 editPostFindRes(data, this.state.posts);
655 this.setState(this.state);
656 } else if (res.op == UserOperation.CreatePostLike) {
657 let data = res.data as PostResponse;
658 createPostLikeFindRes(data, this.state.posts);
659 this.setState(this.state);
660 } else if (res.op == UserOperation.AddAdmin) {
661 let data = res.data as AddAdminResponse;
662 this.state.siteRes.admins = data.admins;
663 this.setState(this.state);
664 } else if (res.op == UserOperation.BanUser) {
665 let data = res.data as BanUserResponse;
666 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
668 // Remove the banned if its found in the list, and the action is an unban
669 if (found && !data.banned) {
670 this.state.siteRes.banned = this.state.siteRes.banned.filter(
671 i => i.id !== data.user.id
674 this.state.siteRes.banned.push(data.user);
678 .filter(p => p.creator_id == data.user.id)
679 .forEach(p => (p.banned = data.banned));
681 this.setState(this.state);
682 } else if (res.op == UserOperation.GetComments) {
683 let data = res.data as GetCommentsResponse;
684 this.state.comments = data.comments;
685 this.state.loading = false;
686 this.setState(this.state);
687 } else if (res.op == UserOperation.EditComment) {
688 let data = res.data as CommentResponse;
689 editCommentRes(data, this.state.comments);
690 this.setState(this.state);
691 } else if (res.op == UserOperation.CreateComment) {
692 let data = res.data as CommentResponse;
694 // Necessary since it might be a user reply
695 if (data.recipient_ids.length == 0) {
696 // If you're on subscribed, only push it if you're subscribed.
697 if (this.state.listingType == ListingType.Subscribed) {
699 this.state.subscribedCommunities
700 .map(c => c.community_id)
701 .includes(data.comment.community_id)
703 this.state.comments.unshift(data.comment);
706 this.state.comments.unshift(data.comment);
708 this.setState(this.state);
710 } else if (res.op == UserOperation.SaveComment) {
711 let data = res.data as CommentResponse;
712 saveCommentRes(data, this.state.comments);
713 this.setState(this.state);
714 } else if (res.op == UserOperation.CreateCommentLike) {
715 let data = res.data as CommentResponse;
716 createCommentLikeRes(data, this.state.comments);
717 this.setState(this.state);