1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
6 Community as CommunityI,
19 AddModToCommunityResponse,
20 BanFromCommunityResponse,
25 WebSocketJsonResponse,
28 } from '../interfaces';
29 import { WebSocketService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { DataTypeSelect } from './data-type-select';
34 import { Sidebar } from './sidebar';
45 createPostLikeFindRes,
50 import { i18n } from '../i18next';
53 community: CommunityI;
55 communityName: string;
56 moderators: Array<CommunityUser>;
57 admins: Array<UserView>;
61 comments: Array<Comment>;
68 export class Community extends Component<any, State> {
69 private subscription: Subscription;
70 private emptyState: State = {
79 number_of_subscribers: null,
80 number_of_posts: null,
81 number_of_comments: null,
88 last_refreshed_at: null,
89 creator_actor_id: null,
94 communityId: Number(this.props.match.params.id),
95 communityName: this.props.match.params.name,
100 dataType: getDataTypeFromProps(this.props),
101 sort: getSortTypeFromProps(this.props),
102 page: getPageFromProps(this.props),
106 creator_id: undefined,
107 published: undefined,
108 creator_name: undefined,
109 number_of_users: undefined,
110 number_of_posts: undefined,
111 number_of_comments: undefined,
112 number_of_communities: undefined,
113 enable_downvotes: undefined,
114 open_registration: undefined,
115 enable_nsfw: undefined,
119 constructor(props: any, context: any) {
120 super(props, context);
122 this.state = this.emptyState;
123 this.handleSortChange = this.handleSortChange.bind(this);
124 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
126 this.subscription = WebSocketService.Instance.subject
127 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
129 msg => this.parseMessage(msg),
130 err => console.error(err),
131 () => console.log('complete')
134 let form: GetCommunityForm = {
135 id: this.state.communityId ? this.state.communityId : null,
136 name: this.state.communityName ? this.state.communityName : null,
138 WebSocketService.Instance.getCommunity(form);
139 WebSocketService.Instance.getSite();
142 componentWillUnmount() {
143 this.subscription.unsubscribe();
146 static getDerivedStateFromProps(props) {
148 dataType: getDataTypeFromProps(props),
149 sort: getSortTypeFromProps(props),
150 page: getPageFromProps(props),
154 componentDidUpdate(_, lastState) {
156 lastState.dataType !== this.state.dataType ||
157 lastState.sort !== this.state.sort ||
158 lastState.page !== this.state.page
160 this.setState({ loading: true });
167 <div class="container">
169 {this.state.loading ? (
171 <svg class="icon icon-spinner spin">
172 <use xlinkHref="#icon-spinner"></use>
177 <div class="col-12 col-md-8">
179 {this.state.community.title}
180 {this.state.community.removed && (
181 <small className="ml-2 text-muted font-italic">
185 {this.state.community.nsfw && (
186 <small className="ml-2 text-muted font-italic">
194 <div class="col-12 col-md-4">
196 community={this.state.community}
197 moderators={this.state.moderators}
198 admins={this.state.admins}
199 online={this.state.online}
200 enableNsfw={this.state.site.enable_nsfw}
210 return this.state.dataType == DataType.Post ? (
212 posts={this.state.posts}
214 sort={this.state.sort}
215 enableDownvotes={this.state.site.enable_downvotes}
216 enableNsfw={this.state.site.enable_nsfw}
220 nodes={commentsToFlatNodes(this.state.comments)}
222 sortType={this.state.sort}
224 enableDownvotes={this.state.site.enable_downvotes}
234 type_={this.state.dataType}
235 onChange={this.handleDataTypeChange}
239 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
242 href={`/feeds/c/${this.state.communityName}.xml?sort=${
243 SortType[this.state.sort]
249 <svg class="icon text-muted small">
250 <use xlinkHref="#icon-rss">#</use>
260 {this.state.page > 1 && (
262 class="btn btn-sm btn-secondary mr-1"
263 onClick={linkEvent(this, this.prevPage)}
268 {this.state.posts.length > 0 && (
270 class="btn btn-sm btn-secondary"
271 onClick={linkEvent(this, this.nextPage)}
280 nextPage(i: Community) {
281 i.updateUrl({ page: i.state.page + 1 });
282 window.scrollTo(0, 0);
285 prevPage(i: Community) {
286 i.updateUrl({ page: i.state.page - 1 });
287 window.scrollTo(0, 0);
290 handleSortChange(val: SortType) {
291 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
292 window.scrollTo(0, 0);
295 handleDataTypeChange(val: DataType) {
296 this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
297 window.scrollTo(0, 0);
300 updateUrl(paramUpdates: {
306 paramUpdates.data_type || DataType[this.state.dataType].toLowerCase();
308 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
309 const page = paramUpdates.page || this.state.page;
310 this.props.history.push(
311 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
316 if (this.state.dataType == DataType.Post) {
317 let getPostsForm: GetPostsForm = {
318 page: this.state.page,
320 sort: SortType[this.state.sort],
321 type_: ListingType[ListingType.Community],
322 community_id: this.state.community.id,
324 WebSocketService.Instance.getPosts(getPostsForm);
326 let getCommentsForm: GetCommentsForm = {
327 page: this.state.page,
329 sort: SortType[this.state.sort],
330 type_: ListingType[ListingType.Community],
331 community_id: this.state.community.id,
333 WebSocketService.Instance.getComments(getCommentsForm);
337 parseMessage(msg: WebSocketJsonResponse) {
339 let res = wsJsonToRes(msg);
341 toast(i18n.t(msg.error), 'danger');
342 this.context.router.history.push('/');
344 } else if (msg.reconnect) {
346 } else if (res.op == UserOperation.GetCommunity) {
347 let data = res.data as GetCommunityResponse;
348 this.state.community = data.community;
349 this.state.moderators = data.moderators;
350 this.state.admins = data.admins;
351 this.state.online = data.online;
352 document.title = `/c/${this.state.community.name} - ${this.state.site.name}`;
353 this.setState(this.state);
355 } else if (res.op == UserOperation.EditCommunity) {
356 let data = res.data as CommunityResponse;
357 this.state.community = data.community;
358 this.setState(this.state);
359 } else if (res.op == UserOperation.FollowCommunity) {
360 let data = res.data as CommunityResponse;
361 this.state.community.subscribed = data.community.subscribed;
362 this.state.community.number_of_subscribers =
363 data.community.number_of_subscribers;
364 this.setState(this.state);
365 } else if (res.op == UserOperation.GetPosts) {
366 let data = res.data as GetPostsResponse;
367 this.state.posts = data.posts;
368 this.state.loading = false;
369 this.setState(this.state);
371 } else if (res.op == UserOperation.EditPost) {
372 let data = res.data as PostResponse;
373 editPostFindRes(data, this.state.posts);
374 this.setState(this.state);
375 } else if (res.op == UserOperation.CreatePost) {
376 let data = res.data as PostResponse;
377 this.state.posts.unshift(data.post);
378 this.setState(this.state);
379 } else if (res.op == UserOperation.CreatePostLike) {
380 let data = res.data as PostResponse;
381 createPostLikeFindRes(data, this.state.posts);
382 this.setState(this.state);
383 } else if (res.op == UserOperation.AddModToCommunity) {
384 let data = res.data as AddModToCommunityResponse;
385 this.state.moderators = data.moderators;
386 this.setState(this.state);
387 } else if (res.op == UserOperation.BanFromCommunity) {
388 let data = res.data as BanFromCommunityResponse;
391 .filter(p => p.creator_id == data.user.id)
392 .forEach(p => (p.banned = data.banned));
394 this.setState(this.state);
395 } else if (res.op == UserOperation.GetComments) {
396 let data = res.data as GetCommentsResponse;
397 this.state.comments = data.comments;
398 this.state.loading = false;
399 this.setState(this.state);
400 } else if (res.op == UserOperation.EditComment) {
401 let data = res.data as CommentResponse;
402 editCommentRes(data, this.state.comments);
403 this.setState(this.state);
404 } else if (res.op == UserOperation.CreateComment) {
405 let data = res.data as CommentResponse;
407 // Necessary since it might be a user reply
408 if (data.recipient_ids.length == 0) {
409 this.state.comments.unshift(data.comment);
410 this.setState(this.state);
412 } else if (res.op == UserOperation.SaveComment) {
413 let data = res.data as CommentResponse;
414 saveCommentRes(data, this.state.comments);
415 this.setState(this.state);
416 } else if (res.op == UserOperation.CreateCommentLike) {
417 let data = res.data as CommentResponse;
418 createCommentLikeRes(data, this.state.comments);
419 this.setState(this.state);
420 } else if (res.op == UserOperation.GetSite) {
421 let data = res.data as GetSiteResponse;
422 this.state.site = data.site;
423 this.setState(this.state);