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 // Necessary for back button for some reason
147 componentWillReceiveProps(nextProps: any) {
149 nextProps.history.action == 'POP' ||
150 nextProps.history.action == 'PUSH'
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 {this.state.loading ? (
166 <svg class="icon icon-spinner spin">
167 <use xlinkHref="#icon-spinner"></use>
172 <div class="col-12 col-md-8">
174 {this.state.community.title}
175 {this.state.community.removed && (
176 <small className="ml-2 text-muted font-italic">
180 {this.state.community.nsfw && (
181 <small className="ml-2 text-muted font-italic">
189 <div class="col-12 col-md-4">
191 community={this.state.community}
192 moderators={this.state.moderators}
193 admins={this.state.admins}
194 online={this.state.online}
195 enableNsfw={this.state.site.enable_nsfw}
205 return this.state.dataType == DataType.Post ? (
207 posts={this.state.posts}
209 sort={this.state.sort}
210 enableDownvotes={this.state.site.enable_downvotes}
211 enableNsfw={this.state.site.enable_nsfw}
215 nodes={commentsToFlatNodes(this.state.comments)}
217 sortType={this.state.sort}
219 enableDownvotes={this.state.site.enable_downvotes}
229 type_={this.state.dataType}
230 onChange={this.handleDataTypeChange}
234 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
237 href={`/feeds/c/${this.state.communityName}.xml?sort=${
238 SortType[this.state.sort]
244 <svg class="icon text-muted small">
245 <use xlinkHref="#icon-rss">#</use>
255 {this.state.page > 1 && (
257 class="btn btn-sm btn-secondary mr-1"
258 onClick={linkEvent(this, this.prevPage)}
263 {this.state.posts.length > 0 && (
265 class="btn btn-sm btn-secondary"
266 onClick={linkEvent(this, this.nextPage)}
275 nextPage(i: Community) {
280 window.scrollTo(0, 0);
283 prevPage(i: Community) {
288 window.scrollTo(0, 0);
291 handleSortChange(val: SortType) {
292 this.state.sort = val;
294 this.state.loading = true;
295 this.setState(this.state);
298 window.scrollTo(0, 0);
301 handleDataTypeChange(val: DataType) {
302 this.state.dataType = val;
304 this.state.loading = true;
305 this.setState(this.state);
308 window.scrollTo(0, 0);
312 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
313 let sortStr = SortType[this.state.sort].toLowerCase();
314 this.props.history.push(
315 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
320 if (this.state.dataType == DataType.Post) {
321 let getPostsForm: GetPostsForm = {
322 page: this.state.page,
324 sort: SortType[this.state.sort],
325 type_: ListingType[ListingType.Community],
326 community_id: this.state.community.id,
328 WebSocketService.Instance.getPosts(getPostsForm);
330 let getCommentsForm: GetCommentsForm = {
331 page: this.state.page,
333 sort: SortType[this.state.sort],
334 type_: ListingType[ListingType.Community],
335 community_id: this.state.community.id,
337 WebSocketService.Instance.getComments(getCommentsForm);
341 parseMessage(msg: WebSocketJsonResponse) {
343 let res = wsJsonToRes(msg);
345 toast(i18n.t(msg.error), 'danger');
346 this.context.router.history.push('/');
348 } else if (msg.reconnect) {
350 } else if (res.op == UserOperation.GetCommunity) {
351 let data = res.data as GetCommunityResponse;
352 this.state.community = data.community;
353 this.state.moderators = data.moderators;
354 this.state.admins = data.admins;
355 this.state.online = data.online;
356 document.title = `/c/${this.state.community.name} - ${this.state.site.name}`;
357 this.setState(this.state);
359 } else if (res.op == UserOperation.EditCommunity) {
360 let data = res.data as CommunityResponse;
361 this.state.community = data.community;
362 this.setState(this.state);
363 } else if (res.op == UserOperation.FollowCommunity) {
364 let data = res.data as CommunityResponse;
365 this.state.community.subscribed = data.community.subscribed;
366 this.state.community.number_of_subscribers =
367 data.community.number_of_subscribers;
368 this.setState(this.state);
369 } else if (res.op == UserOperation.GetPosts) {
370 let data = res.data as GetPostsResponse;
371 this.state.posts = data.posts;
372 this.state.loading = false;
373 this.setState(this.state);
375 } else if (res.op == UserOperation.EditPost) {
376 let data = res.data as PostResponse;
377 editPostFindRes(data, this.state.posts);
378 this.setState(this.state);
379 } else if (res.op == UserOperation.CreatePost) {
380 let data = res.data as PostResponse;
381 this.state.posts.unshift(data.post);
382 this.setState(this.state);
383 } else if (res.op == UserOperation.CreatePostLike) {
384 let data = res.data as PostResponse;
385 createPostLikeFindRes(data, this.state.posts);
386 this.setState(this.state);
387 } else if (res.op == UserOperation.AddModToCommunity) {
388 let data = res.data as AddModToCommunityResponse;
389 this.state.moderators = data.moderators;
390 this.setState(this.state);
391 } else if (res.op == UserOperation.BanFromCommunity) {
392 let data = res.data as BanFromCommunityResponse;
395 .filter(p => p.creator_id == data.user.id)
396 .forEach(p => (p.banned = data.banned));
398 this.setState(this.state);
399 } else if (res.op == UserOperation.GetComments) {
400 let data = res.data as GetCommentsResponse;
401 this.state.comments = data.comments;
402 this.state.loading = false;
403 this.setState(this.state);
404 } else if (res.op == UserOperation.EditComment) {
405 let data = res.data as CommentResponse;
406 editCommentRes(data, this.state.comments);
407 this.setState(this.state);
408 } else if (res.op == UserOperation.CreateComment) {
409 let data = res.data as CommentResponse;
411 // Necessary since it might be a user reply
412 if (data.recipient_ids.length == 0) {
413 this.state.comments.unshift(data.comment);
414 this.setState(this.state);
416 } else if (res.op == UserOperation.SaveComment) {
417 let data = res.data as CommentResponse;
418 saveCommentRes(data, this.state.comments);
419 this.setState(this.state);
420 } else if (res.op == UserOperation.CreateCommentLike) {
421 let data = res.data as CommentResponse;
422 createCommentLikeRes(data, this.state.comments);
423 this.setState(this.state);
424 } else if (res.op == UserOperation.GetSite) {
425 let data = res.data as GetSiteResponse;
426 this.state.site = data.site;
427 this.setState(this.state);