2019-04-29 00:19:04 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
Community as CommunityI,
|
|
|
|
GetCommunityResponse,
|
|
|
|
CommunityResponse,
|
|
|
|
CommunityUser,
|
|
|
|
UserView,
|
|
|
|
SortType,
|
|
|
|
Post,
|
|
|
|
GetPostsForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetCommunityForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
ListingType,
|
2020-02-08 04:05:15 +00:00
|
|
|
DataType,
|
2019-10-19 00:20:27 +00:00
|
|
|
GetPostsResponse,
|
2020-02-01 01:02:20 +00:00
|
|
|
PostResponse,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
BanFromCommunityResponse,
|
2020-02-08 04:05:15 +00:00
|
|
|
Comment,
|
|
|
|
GetCommentsForm,
|
|
|
|
GetCommentsResponse,
|
|
|
|
CommentResponse,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2020-02-08 04:05:15 +00:00
|
|
|
import { WebSocketService } from '../services';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2020-02-08 04:05:15 +00:00
|
|
|
import { CommentNodes } from './comment-nodes';
|
2019-10-21 00:49:13 +00:00
|
|
|
import { SortSelect } from './sort-select';
|
2020-02-08 04:05:15 +00:00
|
|
|
import { DataTypeSelect } from './data-type-select';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { Sidebar } from './sidebar';
|
2020-02-08 04:05:15 +00:00
|
|
|
import {
|
|
|
|
wsJsonToRes,
|
|
|
|
fetchLimit,
|
|
|
|
toast,
|
|
|
|
getPageFromProps,
|
|
|
|
getSortTypeFromProps,
|
|
|
|
getDataTypeFromProps,
|
2020-02-09 04:20:11 +00:00
|
|
|
editCommentRes,
|
|
|
|
saveCommentRes,
|
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeFindRes,
|
|
|
|
editPostFindRes,
|
|
|
|
commentsToFlatNodes,
|
2020-02-08 04:05:15 +00:00
|
|
|
} from '../utils';
|
2019-11-23 00:18:10 +00:00
|
|
|
import { i18n } from '../i18next';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
community: CommunityI;
|
2019-04-05 19:14:54 +00:00
|
|
|
communityId: number;
|
2019-04-25 21:52:18 +00:00
|
|
|
communityName: string;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-21 20:52:55 +00:00
|
|
|
admins: Array<UserView>;
|
2020-02-01 01:02:20 +00:00
|
|
|
online: number;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-04-29 00:19:04 +00:00
|
|
|
posts: Array<Post>;
|
2020-02-08 04:05:15 +00:00
|
|
|
comments: Array<Comment>;
|
|
|
|
dataType: DataType;
|
2019-04-29 00:19:04 +00:00
|
|
|
sort: SortType;
|
|
|
|
page: number;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Community extends Component<any, State> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: State = {
|
|
|
|
community: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
2019-04-03 23:01:20 +00:00
|
|
|
title: null,
|
|
|
|
category_id: null,
|
|
|
|
category_name: null,
|
|
|
|
creator_id: null,
|
|
|
|
creator_name: null,
|
|
|
|
number_of_subscribers: null,
|
|
|
|
number_of_posts: null,
|
2019-04-04 20:53:32 +00:00
|
|
|
number_of_comments: null,
|
2019-04-21 20:52:55 +00:00
|
|
|
published: null,
|
|
|
|
removed: null,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
2019-05-06 16:26:21 +00:00
|
|
|
deleted: null,
|
2019-03-26 18:00:18 +00:00
|
|
|
},
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: [],
|
2019-04-21 20:52:55 +00:00
|
|
|
admins: [],
|
2019-04-08 21:46:09 +00:00
|
|
|
communityId: Number(this.props.match.params.id),
|
2019-04-25 21:52:18 +00:00
|
|
|
communityName: this.props.match.params.name,
|
2020-02-01 01:02:20 +00:00
|
|
|
online: null,
|
2019-04-29 00:19:04 +00:00
|
|
|
loading: true,
|
|
|
|
posts: [],
|
2020-02-08 04:05:15 +00:00
|
|
|
comments: [],
|
|
|
|
dataType: getDataTypeFromProps(this.props),
|
|
|
|
sort: getSortTypeFromProps(this.props),
|
|
|
|
page: getPageFromProps(this.props),
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-29 00:19:04 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-26 18:00:18 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-10-21 00:49:13 +00:00
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
2020-02-08 04:05:15 +00:00
|
|
|
this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-15 22:28:59 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-19 00:20:27 +00:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-03-26 18:00:18 +00:00
|
|
|
() => console.log('complete')
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
let form: GetCommunityForm = {
|
|
|
|
id: this.state.communityId ? this.state.communityId : null,
|
|
|
|
name: this.state.communityName ? this.state.communityName : null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getCommunity(form);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
// Necessary for back button for some reason
|
|
|
|
componentWillReceiveProps(nextProps: any) {
|
2019-10-21 00:49:13 +00:00
|
|
|
if (
|
|
|
|
nextProps.history.action == 'POP' ||
|
|
|
|
nextProps.history.action == 'PUSH'
|
|
|
|
) {
|
2020-02-08 04:05:15 +00:00
|
|
|
this.state.dataType = getDataTypeFromProps(nextProps);
|
|
|
|
this.state.sort = getSortTypeFromProps(nextProps);
|
|
|
|
this.state.page = getPageFromProps(nextProps);
|
2019-10-21 00:49:13 +00:00
|
|
|
this.setState(this.state);
|
2020-02-08 04:05:15 +00:00
|
|
|
this.fetchData();
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
2019-04-20 04:06:25 +00:00
|
|
|
</h5>
|
2019-10-19 00:20:27 +00:00
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-md-8">
|
|
|
|
<h5>
|
|
|
|
{this.state.community.title}
|
|
|
|
{this.state.community.removed && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('removed')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
{this.state.community.nsfw && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('nsfw')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
</h5>
|
|
|
|
{this.selects()}
|
2020-02-08 04:05:15 +00:00
|
|
|
{this.listings()}
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.paginator()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-md-4">
|
|
|
|
<Sidebar
|
|
|
|
community={this.state.community}
|
|
|
|
moderators={this.state.moderators}
|
|
|
|
admins={this.state.admins}
|
2020-02-01 01:02:20 +00:00
|
|
|
online={this.state.online}
|
2019-10-19 00:20:27 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
listings() {
|
|
|
|
return this.state.dataType == DataType.Post ? (
|
|
|
|
<PostListings posts={this.state.posts} removeDuplicates />
|
|
|
|
) : (
|
2020-02-09 04:20:11 +00:00
|
|
|
<CommentNodes nodes={commentsToFlatNodes(this.state.comments)} noIndent />
|
2020-02-08 04:05:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
selects() {
|
|
|
|
return (
|
2019-10-21 00:49:13 +00:00
|
|
|
<div class="mb-2">
|
2020-02-08 04:05:15 +00:00
|
|
|
<DataTypeSelect
|
|
|
|
type_={this.state.dataType}
|
|
|
|
onChange={this.handleDataTypeChange}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<span class="mx-2">
|
|
|
|
<SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
|
|
|
|
</span>
|
2019-12-02 01:21:19 +00:00
|
|
|
<a
|
2019-12-02 06:20:01 +00:00
|
|
|
href={`/feeds/c/${this.state.communityName}.xml?sort=${
|
|
|
|
SortType[this.state.sort]
|
|
|
|
}`}
|
|
|
|
target="_blank"
|
2019-12-02 01:21:19 +00:00
|
|
|
>
|
|
|
|
<svg class="icon mx-2 text-muted small">
|
|
|
|
<use xlinkHref="#icon-rss">#</use>
|
|
|
|
</svg>
|
|
|
|
</a>
|
2019-04-29 00:19:04 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paginator() {
|
|
|
|
return (
|
2019-08-20 21:40:51 +00:00
|
|
|
<div class="my-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('prev')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2020-01-19 21:47:54 +00:00
|
|
|
{this.state.posts.length == fetchLimit && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('next')}
|
2020-01-19 21:47:54 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2019-04-29 00:19:04 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
nextPage(i: Community) {
|
2019-04-29 00:19:04 +00:00
|
|
|
i.state.page++;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
2020-02-08 04:05:15 +00:00
|
|
|
i.fetchData();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
prevPage(i: Community) {
|
2019-04-29 00:19:04 +00:00
|
|
|
i.state.page--;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
2020-02-08 04:05:15 +00:00
|
|
|
i.fetchData();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:49:13 +00:00
|
|
|
handleSortChange(val: SortType) {
|
|
|
|
this.state.sort = val;
|
|
|
|
this.state.page = 1;
|
|
|
|
this.state.loading = true;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.updateUrl();
|
2020-02-08 04:05:15 +00:00
|
|
|
this.fetchData();
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDataTypeChange(val: DataType) {
|
|
|
|
this.state.dataType = val;
|
|
|
|
this.state.page = 1;
|
|
|
|
this.state.loading = true;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.updateUrl();
|
|
|
|
this.fetchData();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl() {
|
2020-02-08 04:05:15 +00:00
|
|
|
let dataTypeStr = DataType[this.state.dataType].toLowerCase();
|
2019-04-29 00:19:04 +00:00
|
|
|
let sortStr = SortType[this.state.sort].toLowerCase();
|
2019-10-19 00:20:27 +00:00
|
|
|
this.props.history.push(
|
2020-02-08 04:05:15 +00:00
|
|
|
`/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
fetchData() {
|
|
|
|
if (this.state.dataType == DataType.Post) {
|
|
|
|
let getPostsForm: GetPostsForm = {
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
sort: SortType[this.state.sort],
|
|
|
|
type_: ListingType[ListingType.Community],
|
|
|
|
community_id: this.state.community.id,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getPosts(getPostsForm);
|
|
|
|
} else {
|
|
|
|
let getCommentsForm: GetCommentsForm = {
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
sort: SortType[this.state.sort],
|
|
|
|
type_: ListingType[ListingType.Community],
|
|
|
|
community_id: this.state.community.id,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getComments(getCommentsForm);
|
|
|
|
}
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-03-26 18:00:18 +00:00
|
|
|
console.log(msg);
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-11-23 00:18:10 +00:00
|
|
|
this.context.router.history.push('/');
|
2019-03-26 18:00:18 +00:00
|
|
|
return;
|
2020-02-04 16:19:05 +00:00
|
|
|
} else if (msg.reconnect) {
|
2020-02-08 04:05:15 +00:00
|
|
|
this.fetchData();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetCommunity) {
|
|
|
|
let data = res.data as GetCommunityResponse;
|
|
|
|
this.state.community = data.community;
|
|
|
|
this.state.moderators = data.moderators;
|
|
|
|
this.state.admins = data.admins;
|
2020-02-01 01:02:20 +00:00
|
|
|
this.state.online = data.online;
|
2019-06-03 01:35:46 +00:00
|
|
|
document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2020-02-08 04:05:15 +00:00
|
|
|
this.fetchData();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.EditCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community = data.community;
|
2019-04-04 22:29:14 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.FollowCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community.subscribed = data.community.subscribed;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.community.number_of_subscribers =
|
2020-01-19 04:54:10 +00:00
|
|
|
data.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetPosts) {
|
|
|
|
let data = res.data as GetPostsResponse;
|
|
|
|
this.state.posts = data.posts;
|
2019-04-29 00:19:04 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
2020-02-01 01:02:20 +00:00
|
|
|
} else if (res.op == UserOperation.EditPost) {
|
|
|
|
let data = res.data as PostResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
editPostFindRes(data, this.state.posts);
|
|
|
|
this.setState(this.state);
|
2020-02-01 01:02:20 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePost) {
|
|
|
|
let data = res.data as PostResponse;
|
|
|
|
this.state.posts.unshift(data.post);
|
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
2020-02-02 17:45:41 +00:00
|
|
|
let data = res.data as PostResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createPostLikeFindRes(data, this.state.posts);
|
2020-02-01 01:02:20 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.AddModToCommunity) {
|
|
|
|
let data = res.data as AddModToCommunityResponse;
|
|
|
|
this.state.moderators = data.moderators;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.BanFromCommunity) {
|
|
|
|
let data = res.data as BanFromCommunityResponse;
|
|
|
|
|
|
|
|
this.state.posts
|
|
|
|
.filter(p => p.creator_id == data.user.id)
|
|
|
|
.forEach(p => (p.banned = data.banned));
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
this.setState(this.state);
|
2020-02-08 04:05:15 +00:00
|
|
|
} else if (res.op == UserOperation.GetComments) {
|
|
|
|
let data = res.data as GetCommentsResponse;
|
|
|
|
this.state.comments = data.comments;
|
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.EditComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
editCommentRes(data, this.state.comments);
|
|
|
|
this.setState(this.state);
|
2020-02-08 04:05:15 +00:00
|
|
|
} else if (res.op == UserOperation.CreateComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
|
|
|
|
|
|
|
// Necessary since it might be a user reply
|
|
|
|
if (data.recipient_ids.length == 0) {
|
|
|
|
this.state.comments.unshift(data.comment);
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
} else if (res.op == UserOperation.SaveComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
saveCommentRes(data, this.state.comments);
|
2020-02-08 04:05:15 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.CreateCommentLike) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createCommentLikeRes(data, this.state.comments);
|
2020-02-08 04:05:15 +00:00
|
|
|
this.setState(this.state);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|